AllenHobson Posted January 4, 2011 Share Posted January 4, 2011 Hello Everyone I am new to php and indeed Web Development. After testing and Playing a bit, I can get the following code to work as two files, the form calling the *.php file to insert into the database, however, I am trying to create one html/php file that displays the form and then executes the php code to insert into the database once user clickes the button. Please can you assist me with the code? I have something horribly wrong and I cannot find it. <?php> <html> <head> <title>Personal Details</title> </head> <body> <form method="post" action="contactdetails.html"><font face="Arial"> Call Sign:<br> <input name="callsign" size="5" type="text"><br> Surame:<br> <input name="surname" size="30" type="text"><br> First Name:<br> <input name="firstnames" size="30" type="text"><br> Known as:<br> <input name="knownas" size="30" type="text"><br> RSA ID No.:<br> <input name="rsaid" size="13" type="text"><br> Birth Date:<br> <input name="birthdate" size="12" type="text"><br> <input name="Insert" value="Next" type="submit"></form> </font><br> </body> </html> //php to insert data into table $callsign = $_POST['callsign']; $surname = $_POST['surname']; $firstnames = $_POST['firstnames']; $knownas = $_POST['knownas']; $rsaid = $_POST['rsaid']; $birthdate = $_POST['birthdate']; mysql_connect ("localhost", "jredpixm_testuse", "PHPDevelopment") or die ('I cannot connect to the database because: ' .mysql_error()); mysql_select_db ("jredpixm_test"); $query="INSERT INTO personal_details (callsign, surname, firstnames, knownas, rsaid, birthdate)Values ('$callsign', '$surname', '$firstnames', '$knownas', '$rsaid', '$birthdate')"; mysql_query($query) or die ('Error updating Database'); echo "<p>Thanks, your information has been added to the database.</p>"; ?> Regards Allen Quote Link to comment https://forums.phpfreaks.com/topic/223405-form-code-and-php-code-in-one-file/ Share on other sites More sharing options...
fxuser Posted January 4, 2011 Share Posted January 4, 2011 Hello Everyone I am new to php and indeed Web Development. After testing and Playing a bit, I can get the following code to work as two files, the form calling the *.php file to insert into the database, however, I am trying to create one html/php file that displays the form and then executes the php code to insert into the database once user clickes the button. Please can you assist me with the code? I have something horribly wrong and I cannot find it. <?php> <html> <head> <title>Personal Details</title> </head> <body> <form method="post" action="contactdetails.html"><font face="Arial"> Call Sign:<br> <input name="callsign" size="5" type="text"><br> Surame:<br> <input name="surname" size="30" type="text"><br> First Name:<br> <input name="firstnames" size="30" type="text"><br> Known as:<br> <input name="knownas" size="30" type="text"><br> RSA ID No.:<br> <input name="rsaid" size="13" type="text"><br> Birth Date:<br> <input name="birthdate" size="12" type="text"><br> <input name="Insert" value="Next" type="submit"></form> </font><br> </body> </html> //php to insert data into table $callsign = $_POST['callsign']; $surname = $_POST['surname']; $firstnames = $_POST['firstnames']; $knownas = $_POST['knownas']; $rsaid = $_POST['rsaid']; $birthdate = $_POST['birthdate']; mysql_connect ("localhost", "jredpixm_testuse", "PHPDevelopment") or die ('I cannot connect to the database because: ' .mysql_error()); mysql_select_db ("jredpixm_test"); $query="INSERT INTO personal_details (callsign, surname, firstnames, knownas, rsaid, birthdate)Values ('$callsign', '$surname', '$firstnames', '$knownas', '$rsaid', '$birthdate')"; mysql_query($query) or die ('Error updating Database'); echo "<p>Thanks, your information has been added to the database.</p>"; ?> Regards Allen i would suggest you leave the html above alone and put below the php code something like <html> stuff </html <?php stuff ?> also if u want it to do the insert if the button is pressed u should do this: if (isset($_POST['submit'])){ //do php stuff } Quote Link to comment https://forums.phpfreaks.com/topic/223405-form-code-and-php-code-in-one-file/#findComment-1154852 Share on other sites More sharing options...
AllenHobson Posted January 4, 2011 Author Share Posted January 4, 2011 Thanks for that I get some test on the page echoing... Please guide me to my mistake you can view the file at http://www.redpixel.co.za/personaldetails.html Here is my latest code see bottom where you suggest the "if (isset($_POST['submit'])){" <?php> <html> <head> <title>Personal Details</title> </head> <body> <form method="post" action="contactdetails.html"><font face="Arial"> Call Sign:<br> <input name="callsign" size="5" type="text"><br> Surame:<br> <input name="surname" size="30" type="text"><br> First Name:<br> <input name="firstnames" size="30" type="text"><br> Known as:<br> <input name="knownas" size="30" type="text"><br> RSA ID No.:<br> <input name="rsaid" size="13" type="text"><br> Birth Date:<br> <input name="birthdate" size="12" type="text"><br> </font><br> </body> </html> //php to insert data into table <input name="Insert" value="Next" type="submit"></form> if (isset($_POST['submit'])){ $callsign = $_POST['callsign']; $surname = $_POST['surname']; $firstnames = $_POST['firstnames']; $knownas = $_POST['knownas']; $rsaid = $_POST['rsaid']; $birthdate = $_POST['birthdate']; mysql_connect ("localhost", "jredpixm_testuse", "PHPDevelopment") or die ('I cannot connect to the database because: ' .mysql_error()); mysql_select_db ("jredpixm_test"); $query="INSERT INTO personal_details (callsign, surname, firstnames, knownas, rsaid, birthdate)Values ('$callsign', '$surname', '$firstnames', '$knownas', '$rsaid', '$birthdate')"; mysql_query($query) or die ('Error updating Database'); echo "<p>Thanks, your information has been added to the database.</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/223405-form-code-and-php-code-in-one-file/#findComment-1154865 Share on other sites More sharing options...
marcus Posted January 4, 2011 Share Posted January 4, 2011 That page should be a PHP document not an HTML document. Quote Link to comment https://forums.phpfreaks.com/topic/223405-form-code-and-php-code-in-one-file/#findComment-1154866 Share on other sites More sharing options...
AllenHobson Posted January 4, 2011 Author Share Posted January 4, 2011 That page should be a PHP document not an HTML document. I then get Parse error: syntax error, unexpected '<' in /home/jredpixm/public_html/personaldetails.php on line 2 Which is: <?php> <html> <head> <title>Personal Details</title> ... Quote Link to comment https://forums.phpfreaks.com/topic/223405-form-code-and-php-code-in-one-file/#findComment-1154871 Share on other sites More sharing options...
MatthewJ Posted January 4, 2011 Share Posted January 4, 2011 A php tag is not an html tag... IE: PHP tags <?php ?> Not <?php> <?> Quote Link to comment https://forums.phpfreaks.com/topic/223405-form-code-and-php-code-in-one-file/#findComment-1154882 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.