Jump to content

Retrieving info from forms.


nathansizemore

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/256787-retrieving-info-from-forms/
Share on other sites

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>

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.