php - Submitting html data and staying on same page -
i have form page
<html> <body> <form action="insert.php" method="post"> app name: <input type="text" name="fname" /><br><br> app id: <input type="text" name="lname" /><br><br> <input type="submit" name="submitbutton"/> </form> </body> </html>
and php page:
<html> <body> <?php $con = mysql_connect("xxx","xxx","xxx"); if (!$con) { die('could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $sql="insert app (fname, lname) values('$_post[fname]','$_post[lname]')"; if (!mysql_query($sql,$con)) { die('error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> </body> </html>
now, want display 1 record added
on same page form , not have form send me new insert.php page it. basically, want submit form stay in same page maybe a new message popping show has worked.
i have looked through answers on stackoverflow using
if(isset($_post['submitbutton']))
but doesn't work. maybe placed wrong or used incorrectly me figure out?
just make action itself, , set if $_post['submit']
add records, , can have both in same file. if wish without refreshing page, you'll need use ajax.
<html> <body> <?php if (isset($_post['submitbutton'])) { $con = mysql_connect("xxx","xxx","xxx"); if (!$con) { die('could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $sql="insert app (fname, lname) values ('$_post[fname]','$_post[lname]')"; if (!mysql_query($sql,$con)) { die('error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) } ?> <form action="" method="post"> app name: <input type="text" name="fname" /><br><br> app id: <input type="text" name="lname" /><br><br> <input type="submit" name="submitbutton"/> </form> </body> </html>
also, mysql_*
deprecated, should consider converting pdo or mysqli avoid sql injection!
Comments
Post a Comment