Jump to content

Cannot get form input


greatdipanshu

Recommended Posts

Hey.. i m a super-newbie, just started on php.

I m using Mysql and Apache 2.2, and everything seems to be working okay.

 

I tried implementing a basic guest-book on my local machine.  For that i used three pages

 

1.) Form.php - Consists of the basic for

2.) process.php - Does the processing

3.) view.php - Views the guestbook

 

The codes to Form.php is

 

<html>
<body>
<h2>Sign my guest book!!!</h2>

<form method=post action="process.php">
<b>Name:</b>
<input type=text size=40 name=name>
<br>
<b>Location:</b>
<input type=text size=40 name=location>
<br>
<b>Email:</b>
<input type=text size=40 name=email>
<br>
<b>Home Page Url:</b>
<input type=text size=40 name=url>
<br>
<b>Comments:</b>
<textarea cols=40 rows=4 wrap=virtual name=comments></textarea>
<br>

<input type=submit name=submit value="Sign!">
<input type=reset name=reset value="Start Over">
</form>
</body>
</html>

 

And the code to process.php is :

 

 

<html>
<body>

<?php
mysql_connect("localhost","root","pwd") or die("Could not connect to database");
mysql_select_db("guestbook") or die("Could not select database");
echo $name;
if($submit=='Sign!')
{
$query = "insert into guestbook(name,location,email,url,comments) values('$name','$location','$email','$url','$comments')";
mysql_query($query) or die(mysql_error());
}
?>

<h2>Thanks!!</h2>
<h2><a href="view.php">View My Guest Book!!!</a></h2>
</body>
</html>

 

 

I tested it.. i ran the form.php..

Once i submit, the second page (process.php) shows

but when i view the contents of the database, there is no change.

And the 'echo $name' in process.php does nothing.

When I remove the if clause, i.e., insert the records without any check, a totally blank record gets appended in the database.

 

So the problem is that the form elements are not getting posted to the process.php script.

 

Help me!!!  :(

Link to comment
https://forums.phpfreaks.com/topic/168374-cannot-get-form-input/
Share on other sites

Try this:

 

<html>
<body>

<?php
mysql_connect("localhost","root","pwd") or die("Could not connect to database");
mysql_select_db("guestbook") or die("Could not select database");
echo $name;
if($submit=='Sign!')
{
$query = "insert into guestbook(name,location,email,url,comments) values($_POST['name'], $_POST['location'], $_POST['email'], $_POST['url'], $_POST['comments'])";
mysql_query($query) or die(mysql_error());
}
?>

<h2>Thanks!!</h2>
<h2><a href="view.php">View My Guest Book!!!</a></h2>
</body>
</html>

 

You're trying to call variables that you haven't declared. Remember, once posted, it's still declared as $_POST['var'].

Thanks for the reply..

but it still behaves the same...

 

Then, I tried this...

 

<html>
<body>

<?php
echo $_POST['name'];
?>
</body>
</html>

 

Still, this doesn't do anything.. i.e. it doesnt display the first field 'name' as i intended it to be.. i get a blank page

Script started.

 

Notice: Undefined index: name in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\process.php on line 8

 

Script finished.

 

So its not getting the fields.. cuz line 8 is where the code tries to access the field..

 

Edit : I thought maybe 'name' is a reserved sortof, but even the other fields give the same error...

Try changing the form (on form.php) to method="get". Also, your HTML is very sloppy. Remember to use quotes and such.

 

Also, try this as process.php

 

<?php

error_reporting(E_ALL);
ini_set('display_errors','1');

echo 'Script started.';
echo '<br />';
echo $_REQUEST['name'];
echo '<br />';
echo 'Script finished.';

?>

 

If that STILL somehow doesn't work, try going to process.php?name=John

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.