Hi - This is my first php project and I'm getting these errors and I'm not sure why.
Notice: Undefined variable: submit in C:\Inetpub\wwwroot\php\db.example5.php on line 8
Notice: Undefined variable: submit in C:\Inetpub\wwwroot\php\db.example5.php on line 27
I'm using PHP Version 4.3.2
Thanks.
***************************
<html>
<body>
<?php
if ($submit) { // this is line 8
// process form
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";
} else{
// display form
?>
<form method="post" action="<?php echo $PHP_SELF?>"> // this is line 27
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Text" name="address"><br>
Position:<input type="Text" name="position"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
} // end if
?>
</body>
</html>
first php project
Moderator: General Moderators
-
rob.weaver
- Forum Newbie
- Posts: 8
- Joined: Tue Apr 22, 2003 11:18 am
- Location: Houston, TX USA
solution
<?php
if (isset($_POST['action']) && $_POST['action'] == 'submitted') {
print '<pre>';
print_r($_POST);
print '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';
print '</pre>';
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Name: <input type="text" name="personal[name]"><br>
Email: <input type="text" name="personal[email]"><br>
Soda: <br>
<select multiple name="soda[]">
<option value="drpepper">Dr. Pepper</option>
<option value="sprite">Sprite</option>
<option value="coke">Coke</option>
</select><br>
<input type="hidden" name="action" value="submitted">
<input type="submit" name="submit" value="submit me!">
</form>
<?php
}
?>
hope it helps
if (isset($_POST['action']) && $_POST['action'] == 'submitted') {
print '<pre>';
print_r($_POST);
print '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';
print '</pre>';
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Name: <input type="text" name="personal[name]"><br>
Email: <input type="text" name="personal[email]"><br>
Soda: <br>
<select multiple name="soda[]">
<option value="drpepper">Dr. Pepper</option>
<option value="sprite">Sprite</option>
<option value="coke">Coke</option>
</select><br>
<input type="hidden" name="action" value="submitted">
<input type="submit" name="submit" value="submit me!">
</form>
<?php
}
?>
hope it helps
Do you have register globals off? That's good if you do.
With register globals off, $submit doesn't exist - hence the undefined variable error message.
You do however have an array - a superglobal array - called $_POST containing all the POST vars. (If register globals was on all the post vars would automatically have been registered in the global scope).
Hence, line 8 should test for:
In fact you often want to test if several POST vars are set before processing the form - perhaps all of them, depending on the task. If you just test for $_POST['submit'], you don't know if anything was actually entered in the form.
Also, it's important to escape string vars (addslashes or mysql_escape_string) before using them in a mysql_query.
Can do the lot in one go with:
With register globals off, $submit doesn't exist - hence the undefined variable error message.
You do however have an array - a superglobal array - called $_POST containing all the POST vars. (If register globals was on all the post vars would automatically have been registered in the global scope).
Hence, line 8 should test for:
Code: Select all
<?php
IF(isset($_POST['submit']))
?>Also, it's important to escape string vars (addslashes or mysql_escape_string) before using them in a mysql_query.
Can do the lot in one go with:
Code: Select all
<?php
foreach ($_POST as $key=>$value)
{
$_POST[$key] = mysql_escape_string($value);
}
?>