hello...
i have textbox with update option. when i change the value in textbox then update the session value. how to assign text value to session variable in javascript.
this is my script.

<script>function update_value()
{
//window.alert(window.document.creditform.quantity.value)

if((window.document.creditform.quantity.value=='') || (window.document.creditform.quantity.value=='0'))
{
window.document.creditform.quantity.value=1;
}
else
{
<? $_SESSION['qty']?>window.document.creditform.quantity.value;
}
}</script>

Recommended Answers

All 5 Replies

actually i have like this on html

<table width="100%" border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td height="35" align="left" valign="top" ><h3 align="left" class="featured_text_black">Your Purchase </h3></td>
          </tr>
          <tr>
            <td align="left" valign="top"><table width="100%" border="0" cellpadding="4" cellspacing="1" bgcolor="#E6E6E6">
              <tr>
                <td width="40%" height="25" align="center" valign="middle" bgcolor="#F2F2F2" class="normal_text2"><strong>Product Name</strong></td>
                <td width="15%" height="25" align="center" valign="middle" bgcolor="#F2F2F2" class="normal_text2"><strong>Quantity</strong></td>
                <td width="15%" height="25" align="center" valign="middle" bgcolor="#F2F2F2" class="normal_text2"><strong>Price</strong></td>
                <td width="15%" height="25" align="center" valign="middle" bgcolor="#F2F2F2" class="normal_text2"><strong>Total</strong></td>
              </tr>
              <tr>
                <td align="center" bgcolor="#FFFFFF" class="normal_text2">abc</td>
                <td align="center" bgcolor="#FFFFFF" class="normal_text2"><input name="quantity" type="text" class="signup_bdr_2" id="quantity" value="1" /><br /><span class="orange_txt"><a class="orange_txt" href="javascript:update_value()"><u>Update</u></a></span></td>
                <td align="center" bgcolor="#FFFFFF" class="normal_text2">$250</td>
                <td align="center" bgcolor="#FFFFFF" class="normal_text2">$<?=(1)*($250)?>//'1' is changes every time when we update.</td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td align="left" valign="top">&nbsp;</td>
          </tr>
        </table>

AFAIK - you can't set the session variable from Javascript. You'll need to either submit the form and set it in your PHP processing code or create an AJAX script that will call a PHP file and update the session variable.

sorry... but I don't seam to understand your problem...
why don't you use JScript for the calculation and up dates, and php to change the session variables?
you create a hidden input form for the new session variable, and upon submit you use is content to update the session variable..

keep it simple ;)

Member Avatar for diafol

sorry... but I don't seam to understand your problem...
why don't you use JScript for the calculation and up dates, and php to change the session variables?
you create a hidden input form for the new session variable, and upon submit you use is content to update the session variable..

keep it simple ;)

Agree - the only advantage I can see for sending a form to a js script is if you have a lot of other interactive js going on - so you need to use Ajax. Otherwise just use form action to a php script/page to handle update.

If you need to use Ajax, I'd recommend using jQuery or prototype:

<a class="orange_txt" href="javascript:update_value();"><u>Update</u></a>

Also change this:

<td align="center" bgcolor="#FFFFFF" class="normal_text2">$<?=(1)*($250)?>

to

<td align="center" bgcolor="#FFFFFF" class="normal_text2" id="update_text">$250</td>

Then in your js (if using jQuery):

function update_value(){
  var sQty = $('#quantity').val();
  var url = "calc_update.php";
  $.post(url,{qty:sQty},function(data){
    $('#update_text').html(data);
  }
}

In your calc_update.php file:

<?php
start_session();
if(isset($_POST['qty'])){
  $qty = $_POST['qty']; 
  if(ctype_digit($qty) || is_integer($qty)){
    $output = $qty * 250;
    $_SESSION['qty'] = $output;
    echo "Updated to \$$output";
  }
}else{
  echo "What the hell are you doing?";
}
?>

I don't seam to understand your problem, sorry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.