Jump to content

Dynamic Form element


tommy1987

Recommended Posts

I have the following code:

 

while ($row = $result->fetch_object()) {
?>
  <tr>
  <td style="font:arial;font-size:14px;font-weight:bold;"><?=$row->name?></td>
  <?php 
  //$lnk = "manageCart('add', ".$row->id.");";
  $nm = $row->id;
  ?> 
  <td style="font:arial;font-size:14px;font-weight:bold;"><?=$money->formatPrice($row->price)?></td>
  <td><input type="text" name="<?=$nm?>" size="1" maxlength="3" value="1" /> <a href="#" onClick="manageCart('add', '<?=$row->id?>', 'document.coolform.<?=$nm?>.value');">Add to Cart</a></td>
  </tr>
<?php
}
?>

 

The problem comes on the line where you see: document.coolform.<?=$nm?>.value'.

 

I know this won't work because PHP is serverside and JS is client side but how can I achieve something like this?

 

Basically I have a long list of all products and a quantity input box and then they click on Add to Cart to add that quantity to the cart. However since the items in the product catalogue come from a DB this is generated dynamically so I cannot possibly manage the name of all the input boxes alongside the Add to Cart buttons so I tried to give them the name of the id of the product to be able to reference it later.

 

I know this probably isn't the clearest post in the world but I hope you get what I mean.

 

Thanks in advance.

Link to comment
Share on other sites

The problem comes on the line where you see: document.coolform.<?=$nm?>.value'.

 

I know this won't work because PHP is serverside and JS is client side but how can I achieve something like this?

 

You can do it like you just did it. PHP passes the javascript from the server to the browser, so it can also pass php variables in the javascript code. Its the opposite that you can't do (pass variables from javascript back to php).

 

ALTHOUGH, you have some sloppy programming styles going on with all your php code that looks like this:

<?=$money->

 

Short tags are bad programming, and if your server updates someday without telling you, these could stop working and you wont even realize. You should be using full php tags:

 

<?php echo $money; ?>

 

 

Link to comment
Share on other sites

Okay so now my code looks something like this:

 

while ($row = $result->fetch_object()) {
?>
  <tr>
  <td style="font:arial;font-size:14px;font-weight:bold;"><?=$row->name?></td>
  <?php 
  //$lnk = "manageCart('add', ".$row->id.");";
  $nm = $row->id;
  ?> 
  <td style="font:arial;font-size:14px;font-weight:bold;"><?=$money->formatPrice($row->price)?></td>
  <td><input type="text" name="<?=$nm?>" size="1" maxlength="3" value="1" /> <a href="#" onClick="manageCart('add', '<?php echo $row->id; ?>', document.coolform.<?php echo $nm;?>.value);">Add to Cart</a> Printed: 
  <script type="text/javascript" language="Javascript">
  document.write(document.coolform.<?php echo $nm;?>.value);
  </script>
  </td>
  </tr>
<?php
}
?>

 

This doesn't even print out the number hence it obviously isn't being fetched properly in javascript. Any ideas anybody?

 

Many thanks in advance.

Link to comment
Share on other sites

My code prints out the correct thing if I print the javascript e.g.

 

document.write('document.coolform.<?php echo $nm; ?>.value');

 

The result will be something like document.coolform.1.value OR document.coolform.2.value etc.]

 

However how must I execute this code and make javascript understand it as code and not a variable (which it is) if you know what I mean.

 

I want to say something like this:

 

  var t = eval('document.write(document.coolform.<?php echo $nm; ?>.value);');

 

Then t contains the actual value of that box which I can use to know what quantity of that item to add to the basket. This doesn't quite work it always sets the variable name to undefined. Ideas?

Link to comment
Share on other sites

Look at your source code - if the value of $nm appears where it should (i.e you have something like document.coolform.1.value in your source code), and your code isn't working, then you have an error in your javascript somewhere. You don't need to do anything special for it to evaluate just because it came from PHP. Your browser doesn't care if its comes from php or if it comes from a static document that has javascript in it. It just looks at the javascript that comes and deals with that. So if the value of $nm is printing out properly, then your problem isnt in the php.

 

If the value of $nm isn't printing out at all, then you have a javascript error somewhere.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.