Free Web Hosting Forum
Go Back   Free Web Hosting Forum > Website Building > Web Programming
Reload this Page PHP Code does not work with dynamically rendered HTML input fields?
Reply
 
Thread Tools Display Modes
(#1 (permalink))
Old
Junior Member
eminkovitch is on a distinguished road
 
Posts: 6
Join Date: Sep 2011
Question PHP Code does not work with dynamically rendered HTML input fields? - 09-07-2011, 01:05 PM

Hi there folks! I was wondering if anyone encountered a similar issue, I will try to explain it.

All the code below works with no errors, except that any HTML input fields that are generated dynamically are not being passed on to the PHP script defined in Action="....." variable. The variables that are dynamically generated are called order_qty1, order_qty2 etc. They are not available to the PHP script via $_POST[...]. I am providing all the code for reference. Static fields work fine, by the way, in the same example. Do you know any workaround for this?

Thanks very much!
Eliott.

------------------------------------------------

Source for the PHP code:

<?php
session_start();
include ('PrintCustAndShipInfo.php');

print('----- This dynamic code does not get the variable -----');print('<br>');
for ( $i=1; $i < 4; $i++ ) {
$ord_qty = $_POST['order_qty'.$i];
print $_POST[$ord_qty];
print '<br>';
}
$var1=1;
print('----- This static code gets the variable -----');print('<br>');
print ($_POST['test'.$var1]);
print '<br>';
?>

<form method="post" action="ProcessCustForm3.php">
<br>Credit Card Information<br>
Name on card: <input type="text" name="credit_card_name"><br>
Card Number: <input type="text" name="credit_card_number"><br>
Expiry date: <input type="text" name="credit_card_expdate"><br>
<input type="submit" value="Finish"><br>
</form>

------------------------------------------------

Source for Dynamic HTML:
<html>
<body>
<form method="post" action="ProcessCustForm2.php">
<?php

session_start();

//now, let's register our session variables
session_register('order_fname');
session_register('order_lname');
session_register('order_email');
session_register('order_phone');
session_register('order_address');
session_register('order_city');
session_register('order_state');
session_register('order_country');
session_register('order_zip');
session_register('order_shipmethod');

//Customer information
$_SESSION['order_fname'] = $_POST['order_fname'];
$_SESSION['order_lname'] = $_POST['order_lname'];
$_SESSION['order_email'] = $_POST['order_email'];
$_SESSION['order_phone'] = $_POST['order_phone'];

// Shipping information
$_SESSION['order_address'] = $_POST['order_address'];
$_SESSION['order_city'] = $_POST['order_city'];
$_SESSION['order_state'] = $_POST['order_state'];
$_SESSION['order_country'] = $_POST['order_country'];
$_SESSION['order_zip'] = $_POST['order_zip'];
$_SESSION['order_shipmethod'] = $_POST['order_shipmethod'];

include( 'PrintCustAndShipInfo.php' );

getInv();

function getInv() {
// setup connection
$mysql_host = "mysql2.000webhost.com";
$mysql_database = "a5270158_order1";
$mysql_user = "a5270158_order1";
$mysql_password = "breathe123";

// connect to db
$con = mysql_connect($mysql_host,$mysql_user,$mysql_passw ord);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
else {
// connect to database
$db_found = mysql_select_db($mysql_database);

if ($db_found) {
// read records from order_inventory table into a list
$sql = "SELECT * from order_inventory";
$result = mysql_query($sql);
$line_num = 1;

print ('<table border=1>');
print ('<tr>'); print("\n");
print ('<td colspan="5">');print("\n");
print ('Select the items that you wish to order');print("\n");
print ('</tr>');print("\n");
while ($db_field = mysql_fetch_assoc($result)) {
$order_qty = '<input type="text" '.'name="order_qty' . $line_num . '" value=0 size=3 maxlength=3>';
print '<tr>'; print("\n");
print '<td>'; print $db_field['id']; print '</td>'; print("\n");
print '<td>'; print $order_qty; print '</td>'; print("\n");
print '<td>'; print $db_field['title']; print '</td>'; print("\n");
print '<td>'; print $db_field['author']; print '</td>'; print("\n");
print '<td>'; print $db_field['price'] ; print '</td>'; print("\n");
print '</tr>'; print("\n");
print '<br>'; print("\n");
$line_num++;
}
print '</table>';
}
else {
print "Database NOT Found"; print ('<p>');
}

// close connection
mysql_close($con);
}
}
?>

<br>

<input type="text" name="test1" value="testing post">
<input type="submit" value="Proceed to step #3">
</form>
</body>
</html>

------------------------------------------------

HTML Rendered by the browser:
<html>
<body>
<form method="post" action="ProcessCustForm2.php">
<table width="60%" border="1"><tr><td>Customer Information</td><td>Shipping Information</td></tr><tr><td><br><br><br>999-999-9999<br></td><td><br><br><br>USA<br><br>1<br></td></tr></table><table border=1><tr>
<td colspan="5">
Select the items that you wish to order
</tr>
<tr>
<td>1</td>
<td><input type="text" name="order_qty1" value=0 size=3 maxlength=3></td>
<td>Alice in Wonderland</td>

<td>Oolon Colluphid</td>
<td>9.99</td>
</tr>
<br>
<tr>
<td>2</td>
<td><input type="text" name="order_qty2" value=0 size=3 maxlength=3></td>
<td>The Secret Garden</td>
<td>Oolon Colluphid</td>
<td>9.99</td>
</tr>

<br>
<tr>
<td>3</td>
<td><input type="text" name="order_qty3" value=0 size=3 maxlength=3></td>
<td>The Wind In The Willows</td>
<td>Oolon Colluphid</td>
<td>9.99</td>
</tr>
<br>
</table>
<br>

<input type="text" name="test1" value="testing post">

<input type="submit" value="Proceed to step #3">
</form>
</body>
</html>
<!-- www.000webhost.com Analytics Code -->
<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>
<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript>
<!-- End Of Analytics Code -->
Reply With Quote
Sponsored Links
(#2 (permalink))
Old
chico's Avatar
Senior Member
chico is on a distinguished road
 
Posts: 341
Join Date: Aug 2011
Default 09-07-2011, 01:27 PM

Seems like your website is under review right now.
Would have liked to see it in action, to completely understand what's going wrong.


Couldn't think of a fancy signature, so you get a instead.
Reply With Quote
(#3 (permalink))
Old
Junior Member
eminkovitch is on a distinguished road
 
Posts: 6
Join Date: Sep 2011
Default Site is accessible now - 09-07-2011, 02:20 PM

Hi Chico, thanks for looking at this... the site is operational right now, you access it by this url:

http://minkosaurus.net76.net/OrderForm/CustForm.html

The third step is where the issue is, it is clearly marked up, so you can see where the fields would normally display the values.

Best regards
Eliott
Reply With Quote
(#4 (permalink))
Old
bigal's Avatar
Senior Member
bigal is on a distinguished road
 
Posts: 183
Join Date: Mar 2009
Location: Brisbane, Australia
Default 09-08-2011, 01:55 AM

PHP Code:
for ( $i=1$i 4$i++ ) {
$ord_qty $_POST['order_qty'.$i];
print 
$_POST[$ord_qty];
print 
'<br>'
No need to put $_POST around $ord_qty,
just

PHP Code:
for ( $i=1$i 4$i++ ) {
$ord_qty $_POST['order_qty'.$i];
print 
$ord_qty;
print 
'<br>'
Reply With Quote
(#5 (permalink))
Old
Junior Member
eminkovitch is on a distinguished road
 
Posts: 6
Join Date: Sep 2011
Default 09-08-2011, 01:06 PM

I tried it that way too, no success. Anyway, I found a workaround, but I don't believe that you can send fields that are generated by dynamic HTML. Thanks for your reply!
Reply With Quote
(#6 (permalink))
Old
chico's Avatar
Senior Member
chico is on a distinguished road
 
Posts: 341
Join Date: Aug 2011
Default 09-08-2011, 01:23 PM

Quote:
Originally Posted by eminkovitch View Post
Hi Chico, thanks for looking at this... the site is operational right now, you access it by this url:

http://minkosaurus.net76.net/OrderForm/CustForm.html

The third step is where the issue is, it is clearly marked up, so you can see where the fields would normally display the values.

Best regards
Eliott
It still seems to be under review.


Couldn't think of a fancy signature, so you get a instead.
Reply With Quote
(#7 (permalink))
Old
bigal's Avatar
Senior Member
bigal is on a distinguished road
 
Posts: 183
Join Date: Mar 2009
Location: Brisbane, Australia
Default 09-09-2011, 12:23 AM

Quote:
Originally Posted by eminkovitch View Post
I tried it that way too, no success. Anyway, I found a workaround, but I don't believe that you can send fields that are generated by dynamic HTML. Thanks for your reply!
No worries, but I am sure the passing of HTML name value passing to PHP works as I ran with below test code

PHP Code:
<?php
//TEST 1
if (isset($_POST['order_qty1'])){
   for(
$i=1;$i<4;$i++){
    
$orders 'order_qty'.$i;
    print 
$_POST[$orders];
   }
}   
  echo 
'<br />';
//TEST 2
if (isset($_POST['order_qty1'])){
   for ( 
$i=1$i 4$i++ ) {
    
$ord_qty $_POST['order_qty'.$i];
    print 
$ord_qty;
    
   }
}   

echo 
'
<form action="'
.$_SERVER['PHP_SELF'].'" method="post" />
<input type="text" name="order_qty1" value=1 size=3 maxlength=3><br />
<input type="text" name="order_qty2" value=2 size=3 maxlength=3><br />
<input type="text" name="order_qty3" value=3 size=3 maxlength=3><br />
<input type="submit" value="submit" />
</form>'
;


?>
Reply With Quote
Reply

Tags
$_post, dynamic html, php

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com