View Full Version : Making PHP insert code in the same page with the html code...
heavensgate15
06-01-2009, 11:37 AM
hey, I want to know whether it's possible to write your PHP insert code together with your HTML code where your forms are created...... what I mean is something like this:
<?php
//code for inserting the data found in the forms from the html code below
?>
<html>
<head>
<title> Inserting data from forms to the database</title>
</head>
<body>
<form action="nextpage.php" method="POST">
//input tags
</form>
</body>
</html>
You know, I tried this one but when I checked out my database after executing this, nothing has been added to my database... But I saw some of my classmates having this kind of codes... I've already asked them, but they cannot explain to me why... I feel like, this kind of code will not work if I'm the one who's gonna write it... please help... T_T
You can only do that once the user presses the submit button, take some look in the links below:
http://www.google.com/search?hl=en&rlz=1C1GGLS_enUS323US323&ei=f8AjSq7pJZbCMrTJkb0J&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=getting+form+data+in+php&spell=1
heavensgate15
06-01-2009, 12:38 PM
ahw... I looked all the links that you gave... I didn't find something that could answer my question....
if you're asking how to write both the form and the form processing code on the same page, try this:
<?php
if (!isset($_POST['submit'])) { //if form is not submitted,
echo '<!--form html code goes here--><input type="submit" name="submit" value="submit">'; //show the form.
} else { //otherwise,
//php code to process form goes here
}
?>
forum_amnesiac
06-01-2009, 04:34 PM
Likewise you can do this, in a php file, when you have retrieved the data you want.
<?
PHP code here that creates a variable $php_variable
?>
<html>
......
<input type="text" value='<? echo "$php_variable"?>'><? echo "php_variable" ?>
......
</html>
heavensgate15
06-02-2009, 01:24 PM
thanks for the reply.. but I want to know how my code above is executed... what will happen if my php code for inserting data from forms is placed above my html code? Is my php code for inserting data is executed then after the user clicks the submit button? or it is not? What would be the case if I put it after my html code? Is the result would be the same?
forum_amnesiac
06-02-2009, 02:55 PM
It doesn't matter where you put the PHP in relation to the HTML, the PHP will always execute first.
That is why in your HTML form you have method="POST" and action="someaction.php", this way your HTML can get the field variables that PHP can then process.
However, if you use the "if - else" method I posted above, then it is actually the php code that is writing the html. So, the php will choose to either write the form OR process the submission, based on whether or not the form has been submitted already - but it won't do both.
It might be easier for you to think of the two steps not as two parts of the same script, but as two different scripts that do different parts of the job. The form and the code to process the form could easily be on separate pages.
heavensgate15
06-03-2009, 05:04 PM
hmm... I understand what you mean... But still, there are lots of question in my mind... hmm, let's put it like this... I have a simple php scripts:
simple.php
<?php
include('dbconnect.php');
$name = $_POST[name];
$age = $_POST[age];
$data = "insert into simplerecord values(null,'$name','$age')";
mysql_query($data) or die(mysql_error());
?>
<html>
<head>
<title> simple form </title>
</head>
<body>
<form action="simple2.php" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="name"></p>
<p><strong>Age:</strong><br>
<input type="text" maxlength="2" name="age"></p>
<p><input type="submit" value="send"></p>
</form>
</body>
</html>
simple2.php
<?php
include('dbconnect.php');
$data = "select * from simplerecord";
$query = mysql_query($data);
$num = mysql_num_rows($query);
$x = 0;
while($x < $num)
{
$name = mysql_result($query,$x,"name");
$ID = mysql_result($query,$x,"ID");
$age = mysql_result($query,$x,"age");
printf("Name: %s<br>",$name);
printf("ID: %d<br>",$ID);
printf("Age: %d<br><br>",$age);
$x++;
}
?>
dbconnect.php
<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("db",$con);
?>
let's say, these 3 scripts create an application...These are the things that I found out.... First is the simple.php... In this script, when I didn't put a value on the action attribute, the process seems to be ok, I successfully added a record (simple2.php is not yet created)... so the form tag looks like this:
<form action="" method="POST">
When I created the simple2.php, the form tag in simple.php script looks like this:
<form action="simple2.php" method="POST">
But this time, when I test it through my browser(filling up the name and age) and checked my database, the record that I inserted become null in my database (the attributes name and age has no value in my database but the attribute ID has a value)... Why is that?
OUTPUT in browser when simple2.php was not yet created: // I put display code in the same page
Name: lourence john cabaluna
ID: 1
Age: 18
Attributes values in the database when simple2.php was not yet created:
ID = 1
name = lourence john cabaluna
age = 18
OUTPUT in browser when simple2.php was created:
Name: lourence john cabaluna
ID: 1
Age: 18
Name:
ID: 2
Age: 0
Attributes values in the database when simple2.php was created:
ID = 1
name = lourence john cabaluna
age = 18
ID = 2
name =
age = 0
heavensgate15
06-07-2009, 10:37 AM
kindly help me about this.. it's still bugging me....
well, I'm still learning a lot of mysql, so I'm not sure why it appears that a new, blank record is being inserted. It seems to me that *nothing* new should show up. The reason that your form is no longer inputting new records like you want is that the code for doing it:
<?php
include('dbconnect.php');
$name = $_POST[name];
$age = $_POST[age];
$data = "insert into simplerecord values(null,'$name','$age')";
mysql_query($data) or die(mysql_error());
?>
is still in "simple.php", which is no longer the file your form uses (when you had no "action" defined, the server defaulted to using the same page as the form). "simple2.php" doesn't reference any POST data (from the form) at all. When you specify that the form uses "simple2.php", you have to put all the code you want the form to use in "simple2.php" also.
You will end up with conflicting variables, so you'll have to rename them when you rewrite "simple2.php".
heavensgate15
06-09-2009, 11:05 AM
hmmm, it is troublesome if I rewrite my php code from the simple.php to simple2.php because it is redundant.... is there a way that you can go to another page without the submit button? what i mean is something like this:
<?php
include('dbconnect.php');
$data = "insert into table_name values(null,'name','age')";
$query = mysql_query($data);
if(!$query)
{
die(mysql_error());
}
else
{
// php code/built-in function to go to another page
}
?>
is there something like that in php? a function or a code to go to another page? What I think is that, I will separate my codes; one for getting the data through forms and put it in a file named form.html. Then one for inserting the data from form.html and put it in file named insert.php (the code for this is supposedly something like above), and another file named display.php for displaying the data from the database. Or, is there another way in writing a code in a way that after the user inputs the value, the value will be inserted to the database after the user clicks the submit button, then when the user clicked the submit button, it will go to another page?
okay...:
this
simple.php
<?php
include('dbconnect.php');
$name = $_POST[name];
$age = $_POST[age];
$data = "insert into simplerecord values(null,'$name','$age')";
mysql_query($data) or die(mysql_error());
?>
does the query and inserts a record. However, because the form action is now "simple2.php", it does not take data from the form. The record it inserts is blank.
Adding that code (or, more specifically, *moving* that code) to simple2.php would not be redundant because the code no longer works properly where it is anyway. You will end up with extra blank records every time someone visits the page, whether they fill out the form or not.
Am I correct that you want to display the simplerecord table after someone submits their record to it? Try something like this (all on one page):
simple.php
<?php
if(!isset($_POST['name'])){
echo '
<html>
<head>
<title> simple form </title>
</head>
<body>
<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="name"></p>
<p><strong>Age:</strong><br>
<input type="text" maxlength="2" name="age"></p>
<p><input type="submit" value="send"></p>
</form>
</body>
</html>';
}else{
include('dbconnect.php');
$name = $_POST[name];
$age = $_POST[age];
$data = "insert into simplerecord values(null,'$name','$age')";
mysql_query($data) or die(mysql_error());
$data2 = "select * from simplerecord";
$query2 = mysql_query($data2);
$num2 = mysql_num_rows($query2);
$x = 0;
while($x < $num2)
{
$name2 = mysql_result($query2,$x,"name");
$ID2 = mysql_result($query2,$x,"ID");
$age2 = mysql_result($query2,$x,"age");
printf("Name: %s<br>",$name2);
printf("ID: %d<br>",$ID2);
printf("Age: %d<br><br>",$age2);
$x++;
}
}
?>
OR, you could remove the if - else statement and keep the code on two pages. only the form would go on "simple.php" (with action="simple2.php"), everything else would go on "simple2.php".
heavensgate15
06-11-2009, 03:06 AM
I think, I get your point... when the user visit the page for the first time, the code that is executed is the if statement right? because the submit button is not yet clicked. So, when the user clicked the submit button, the data that he/she inputs will be process in the else statement because the if condition is no longer true.
But I want that, after the user clicks the button, it will go to another page. So probably, the code for that will be put in the else part I think? But I don't know what code or functions will be used so that it will go to another page....:confused:
you can break it up into two pages if you prefer.
####simple.php####
<html>
<head>
<title> simple form </title>
</head>
<body>
<form action="simple2.php" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="name"></p>
<p><strong>Age:</strong><br>
<input type="text" maxlength="2" name="age"></p>
<p><input type="submit" value="send"></p>
</form>
</body>
</html>
####simple2.php####
<?php
include('dbconnect.php');
$name = $_POST[name];
$age = $_POST[age];
$data = "insert into simplerecord values(null,'$name','$age')";
mysql_query($data) or die(mysql_error());
$data2 = "select * from simplerecord";
$query2 = mysql_query($data2);
$num2 = mysql_num_rows($query2);
$x = 0;
while($x < $num2)
{
$name2 = mysql_result($query2,$x,"name");
$ID2 = mysql_result($query2,$x,"ID");
$age2 = mysql_result($query2,$x,"age");
printf("Name: %s<br>",$name2);
printf("ID: %d<br>",$ID2);
printf("Age: %d<br><br>",$age2);
$x++;
}
?>
if you just want a redirect, I think the simplest way would be via javascript. Put it at the end of your php code:
<?php
echo '
<script type="text/javascript">
window.location = "http://www.yourdomain.com/otherpage.php"
</script>';
?>
heavensgate15
06-11-2009, 12:27 PM
Thank you for your code... I got an idea on how to do this now hehe, and it works... Thanks a million....:)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.