Jump to content

form code and php code in one file


AllenHobson

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/223405-form-code-and-php-code-in-one-file/
Share on other sites

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
}

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>";   
}

?>

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>
...

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.