nathansizemore Posted February 10, 2012 Share Posted February 10, 2012 Hello, I have a few questions. First, this is what I am trying to accomplish - I am trying to take values that are entered into forms. And then store them into a database. My plan was to take them into an array, and create a loop that wrote the values to the database. So, the first step I thought would be to learn how to take info from a form, then display it onto the screen. Theoretically, if I could do that, I could just learn the MySQL commands to write to the database, and paste them instead of printing to the screen. So, I gave it a go. It didn't work. The way the 'Key' is chosen with arrays in PHP was different than I thought. I gave my sample code to a friend and asked for his help. He gave me a working copy back, but signed off before I could ask questions :-\ This is what I am working with: <html> <body> <form name = "barInfo" method = "post"> Establishment name: <input type = "text" name = "EstablishmentName"> <br> Street Address: <input type = "text" name = "StreetAddress"> <br> City: <input type = "text" name = "City"> <br> State: <input type = "text" name = "State"> <br> Zip: <input type = "text" name = "Zip"> <br> <input type="submit" name="Submit" value="Submit"> </form> <?php if (!empty($_POST['EstablishmentName'])) { <br> print "Now, lets see if this shit works..."; <br> print "Establishment Name:" . $_POST['EstablishmentName']; <br> print "Street Address:" . $_POST['StreetAddress']; <br> print "City:" . $_POST['City']; <br> print "State:" . $_POST['State']; <br> print "Zip:" . $_POST['Zip']; <br> } ?> </body> </html> Ok, first off I get a parse error or something like that when I try it. My book has '<br>' thrown all over the place inside '<?php>' things. How can I do an endl; type thing in PHP? Obv <br> does not work., print "\n"; Does nothing, either....? Second, the reason my code didn't display anything, but his did, was because I had a loop that displayed $details[$x], and 1 was added to $x every time it looped. Am I right in saying that during the html part of the code, whatever value is assigned to 'name = ', is the key pointing to that value in my array? If that is true, how would I make that become an integer and use it with a loop. Do I have to put: name = "1", name = "2", etc... ? Thanks in advance for the help! Sorry if I am not referring to terms correctly, I just started looking at HTML/PHP last week... Quote Link to comment https://forums.phpfreaks.com/topic/256787-retrieving-info-from-forms/ Share on other sites More sharing options...
litebearer Posted February 10, 2012 Share Posted February 10, 2012 Take a look here http://www.tizag.com/phpT/forms.php NOTE: in the form ACTION=. Try to adapt to your code Quote Link to comment https://forums.phpfreaks.com/topic/256787-retrieving-info-from-forms/#findComment-1316420 Share on other sites More sharing options...
delickate Posted February 10, 2012 Share Posted February 10, 2012 hi, when you submit the form. it itself return an array. you can use that array to fulfill your requirements. To check that array on form submit, just do the following: print_r($_POST); Hope it'll help you. Quote Link to comment https://forums.phpfreaks.com/topic/256787-retrieving-info-from-forms/#findComment-1316447 Share on other sites More sharing options...
QuickOldCar Posted February 10, 2012 Share Posted February 10, 2012 The reason why are getting parse errors is that you had html inside php code on the new lines. Whatever you add on a new line that you would like to show should use echo or print, the content quoted or properly escaped, and a semicolon at the end of the line. And variables do not need to be quoted, but can be quoted. Something like this. <html> <body> <form name = "barInfo" method = "post"> Establishment name: <input type = "text" name = "EstablishmentName"> <br> Street Address: <input type = "text" name = "StreetAddress"> <br> City: <input type = "text" name = "City"> <br> State: <input type = "text" name = "State"> <br> Zip: <input type = "text" name = "Zip"> <br> <input type="submit" name="Submit" value="Submit"> </form> <?php if (!empty($_POST['EstablishmentName'])) { echo '<br />'; print "Now, lets see if this shit works..."; echo '<br />'; print "Establishment Name:" . $_POST['EstablishmentName']; echo '<br />'; print "Street Address:" . $_POST['StreetAddress']; echo '<br />'; print "City:" . $_POST['City']; echo '<br />'; print "State:" . $_POST['State']; echo '<br />'; print "Zip:" . $_POST['Zip']; echo '<br />'; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/256787-retrieving-info-from-forms/#findComment-1316460 Share on other sites More sharing options...
nathansizemore Posted February 10, 2012 Author Share Posted February 10, 2012 Thanks for all the help guys! Few questions... If I wanted to accept the value written into the fields into an array, would I do something like this: Establishment name: <input type = "text" name = "details[]"> <br> Street Address: <input type = "text" name = "details[]"> <br> City: <input type = "text" name = "details[]"> <br> State: <input type = "text" name = "[details[]"> <br> To display it, I would like to make it a loop... $x = 0; while ($x < $details.endofarray()) { print $details[$x]; echo "<br/>"; $x++; } What exactly do I need in the name = field to be able to call that value with a variable key? Also, is there an 'End of Array' function in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/256787-retrieving-info-from-forms/#findComment-1316831 Share on other sites More sharing options...
nathansizemore Posted February 10, 2012 Author Share Posted February 10, 2012 Also, does name = Just create a pointer? Quote Link to comment https://forums.phpfreaks.com/topic/256787-retrieving-info-from-forms/#findComment-1316833 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.