roonnyy Posted April 4, 2007 Share Posted April 4, 2007 Hi guys i need help with my form. First file post.php take record and send it to post2.php. Now records are insert into databse without submit button press and that is my problem. I need working submit button. When user click on submit button=insert data to database. Sorry for my english. Thanks George <html> <form action="post2.php" method="post"> <form> <ul>Name: <input type="text" name="name" /></ul> <ul>Surname: <input type="text" name="surname" /></ul> <input type="submit" /> </form> </html> <html> <head> <style type="text/css" media="screen">@import "css/style2.css";</style> </head> <body> <?php /* insert record check */ if (strlen($name) <2) { die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte jméno. Bez vloženého jména nelze přihlášku odeslat.</font></p>"); } if (strlen($name) >10) { die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte jméno. Bez vloženého jména nelze přihlášku odeslat.</font></p>"); } if (strlen($name) == 0 ) { die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte jméno. Bez vloženého jména nelze přihlášku odeslat.</font></p>"); } if (strlen($surname) <2) { die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte příjmení. Bez vloženého příjmení nelze přihlášku odeslat.</font></p>"); } if (strlen($surname) >10) { die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte příjmení. Bez vloženého příjmení nelze přihlášku odeslat.</font></p>"); } if (strlen($surname) == 0 ) { die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte příjmení. Bez vloženého příjmení nelze přihlášku odeslat.</font></p>"); } ?> <H2>You write:</H2> <strong>Name</strong> <?php echo $_POST["name"]; ?><br/> <strong>Surname</strong> <?php echo $_POST["surname"]; ?><br/> [color=yellow]<input type="submit" value="submit"/>[/color] <?php /*this is my problem */ if($susbmit) { /*connection to DB */ $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("formular", $con); $sql="INSERT INTO formular (name, surname) VALUES ('$_POST[name]','$_POST[surname]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); /*this belong to if($submit) */ } echo "<br>Your record was added."; mysql_close($con) } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/45515-solved-php-form-add-submit-button-for-insert-records-to-database-how-to-do-this/ Share on other sites More sharing options...
btherl Posted April 4, 2007 Share Posted April 4, 2007 Try if ($_POST['submit'] === 'submit') { And ensure your form has <input type="submit" name="submit" value="submit"> Name: Name of the submit button in php ($_POST['submit']) Value: What to display on the submit button. Also the *value* of $_POST['submit'] Type: This says it's a submit button, and not something else Note that if you use javascript's submit(), you may have trouble with naming your submit button "submit" as well. Quote Link to comment https://forums.phpfreaks.com/topic/45515-solved-php-form-add-submit-button-for-insert-records-to-database-how-to-do-this/#findComment-220985 Share on other sites More sharing options...
roonnyy Posted April 4, 2007 Author Share Posted April 4, 2007 Thanks for fast reply! So i done what you told me. But there is a trouble: Error: Query was empty. Php don't see inserted records from post.php. Why? Here is my updated code that causes error: only change end form post.php: <strong>name</strong> <?php echo $_POST["name"]; ?><br/> <strong>surname</strong> <?php echo $_POST["surname"]; ?><br/> <input type="submit" name="submit" value="submit"> post2.php <?php /*connect to DB */ $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } if ($_POST['submit'] === 'submit') { mysql_select_db("formular", $con); $sql="INSERT INTO formular (name, surname) VALUES ('$_POST[name]','$_POST[surname]')"; } if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "<br>."; mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/45515-solved-php-form-add-submit-button-for-insert-records-to-database-how-to-do-this/#findComment-220989 Share on other sites More sharing options...
jitesh Posted April 4, 2007 Share Posted April 4, 2007 <html> <form action="post2.php" method="post"> <form> <ul>Name: <input type="text" name="name" /></ul> <ul>Surname: <input type="text" name="surname" /></ul> <input type="button" onclick"javascript:this.form.submit();" /> </form> </html> Quote Link to comment https://forums.phpfreaks.com/topic/45515-solved-php-form-add-submit-button-for-insert-records-to-database-how-to-do-this/#findComment-220992 Share on other sites More sharing options...
roonnyy Posted April 4, 2007 Author Share Posted April 4, 2007 thanks for reply but stiil don't work :'( Quote Link to comment https://forums.phpfreaks.com/topic/45515-solved-php-form-add-submit-button-for-insert-records-to-database-how-to-do-this/#findComment-221061 Share on other sites More sharing options...
jitesh Posted April 4, 2007 Share Posted April 4, 2007 <html> <form action="post2.php" method="post" name="frm"> <form> <ul>Name: <input type="text" name="name" /></ul> <ul>Surname: <input type="text" name="surname" /></ul> <input type="button" onclick"javascript:document.frm.submit();" /> </form> </html> Quote Link to comment https://forums.phpfreaks.com/topic/45515-solved-php-form-add-submit-button-for-insert-records-to-database-how-to-do-this/#findComment-221074 Share on other sites More sharing options...
roonnyy Posted April 4, 2007 Author Share Posted April 4, 2007 You misunderstood me. What you post,it already works. That is not trouble. My trouble is: that record are insert to database withnout user confirmation (submit button) I have 2 files ------------------- 1. post.php with FORM (this one take information from user and show it with post2.php) 2. post2.php (show records from user whitch user add in post.php and have to insert these records to database BUT AFTER user CLICK ON SUBMIT BUTTON-this do not work, data are insert without submit button click) everithing working fine but data are insert to database withnout user confirmation (submit button), so i want just this. this is code of post2.php <html> <head> <style type="text/css" media="screen">@import "css/style2.css";</style> </head> <body> <H2>You wrote:</H2> <strong>Name</strong> <?php echo $_POST["name"]; ?><br/> <strong>Surname</strong> <?php echo $_POST["surname"]; ?><br/> <input type="submit" name="submit" value="submit"> <? /*connection to database */ $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("formular", $con); if ($_POST['submit'] === 'submit') { $sql="INSERT INTO formular (name, surname) VALUES ('$_POST[name]','$_POST[surname]')"; } if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "<br>Record was inserted."; mysql_close($con) ?> </body> </html> Thanks everynone for help Quote Link to comment https://forums.phpfreaks.com/topic/45515-solved-php-form-add-submit-button-for-insert-records-to-database-how-to-do-this/#findComment-221086 Share on other sites More sharing options...
grimmier Posted April 4, 2007 Share Posted April 4, 2007 change this $sql="INSERT INTO formular (name, surname) VALUES ('$_POST[name]','$_POST[surname]')"; to this $sql="INSERT INTO formular (name, surname) VALUES ('$_POST['name']','$_POST['surname']')"; Quote Link to comment https://forums.phpfreaks.com/topic/45515-solved-php-form-add-submit-button-for-insert-records-to-database-how-to-do-this/#findComment-221089 Share on other sites More sharing options...
jitesh Posted April 4, 2007 Share Posted April 4, 2007 if (strlen($surname) >10) { die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte příjmení. Bez vloženého příjmení nelze přihlášku odeslat.</font></p>"); } if (strlen($surname) == 0 ) { die("<p align='center'><font face='Arial' size='2' color='#000000'>Vložte příjmení. Bez vloženého příjmení nelze přihlášku odeslat.</font></p>"); } ?> <H2>You write:</H2> <strong>Name</strong> <?php echo $_POST["name"]; ?><br/> <strong>Surname</strong> <?php echo $_POST["surname"]; ?><br/> <form action="post2.php" method="post" name="frm"> <input type="hidden" name="name" value="<?php echo $_POST["name"];?>"> <input type="hidden" name="surname" value="<?php echo $_POST["surname"];?>"> <input type="submit" value="submit"/> </form> <?php /*this is my problem */ if($susbmit) { /*connection to DB */ $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/45515-solved-php-form-add-submit-button-for-insert-records-to-database-how-to-do-this/#findComment-221092 Share on other sites More sharing options...
roonnyy Posted April 4, 2007 Author Share Posted April 4, 2007 Thanks for reply. But still don't work. My code is now little bit messy. So i start again with white list. Do you know some good php and mysql tutorial to FORM WITH PREVIEW and database insert? Quote Link to comment https://forums.phpfreaks.com/topic/45515-solved-php-form-add-submit-button-for-insert-records-to-database-how-to-do-this/#findComment-221109 Share on other sites More sharing options...
roonnyy Posted April 5, 2007 Author Share Posted April 5, 2007 so I start again and this is my working code: thanks to everyone who help solve this out file 1 <html> <form action="post_2.php" method="post"> <form> <ul>Name: <input type="text" name="name" /></ul> <ul>Surname: <input type="text" name="surname" /></ul> <input type="submit" /> </form> file 2 <html> <head> <style type="text/css" media="screen">@import "css/style2.css";</style> </head> <body> <?php /* records check */ if (strlen($name) <2) { die("<p align='center'><font face='Arial' size='2' color='#000000'>wrong name.</font></p>"); } if (strlen($name) >10) { die("<p align='center'><font face='Arial' size='2' color='#000000'>wrong name</font></p>"); } if (strlen($name) == 0 ) { die("<p align='center'><font face='Arial' size='2' color='#000000'>wrong name</font></p>"); } if (strlen($surname) <2) { die("<p align='center'><font face='Arial' size='2' color='#000000'>wrong surname</font></p>"); } if (strlen($surname) >10) { die("<p align='center'><font face='Arial' size='2' color='#000000'>wrong surname</font></p>"); } if (strlen($surname) == 0 ) { die("<p align='center'><font face='Arial' size='2' color='#000000'>wrong surname</font></p>"); } ?> <H2>You wrote:</H2> <strong>Name</strong> <?php echo $_POST["name"]; ?></br> <strong>Surname</strong> <?php echo $_POST["surname"]; ?> <form action="post_jura3.php" method="post" name="frm"> <input type="hidden" name="name" value="<?php echo $_POST["name"];?>"> <input type="hidden" name="surname" value="<?php echo $_POST["surname"];?>"> <input type="submit" name="submit" value="submit"/> </form> </body> </html> file 3 <?php /*connection to DB */ $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } if ($submit) { mysql_select_db("formular", $con); $sql="INSERT INTO formular (name, surname) VALUES ('$_POST[name]','$_POST[surname]')"; } if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "<br>Your record was added."; mysql_close($con) ?> </html> Quote Link to comment https://forums.phpfreaks.com/topic/45515-solved-php-form-add-submit-button-for-insert-records-to-database-how-to-do-this/#findComment-222108 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.