jagguy
asked on
redirect insert data
Hi,
I have a form in html and I enter the data via php and it all works.
What I want is to click the submit button and simply have something appear saying data saved instead of redirecting to another page.
If someone saves data I want them to continue to be able to enter more data and save again. The form is a record of work and not a feedback form.
I have a form in html and I enter the data via php and it all works.
What I want is to click the submit button and simply have something appear saying data saved instead of redirecting to another page.
If someone saves data I want them to continue to be able to enter more data and save again. The form is a record of work and not a feedback form.
<form action="addData2.php" method="post">
Value1: <input type="text" name="one"><br>
Value2: <input type="text" name="two"><br>
Value3: <input type="text" name="three"><br>
<input type="Submit">
</form>
<?php
$one=$_POST['one'];
$two=$_POST['two'];
$three=$_POST['three'];
//$link = mysql_connect('localhost', 'jagguy', 'mexican');
$link = mysql_connect('localhost', 'root');
//$link=mysql_connect("localhost", "jagguy", "password") or die(mysql_error());
@mysql_select_db('db_jagguy');
$query = "INSERT INTO tb_one VALUES ('$one','$two','$three')";
mysql_query($query);
mysql_close();
?>
Don't close the database connect if you want to post again the form add meta refresh tag and refresh page with in certain interval like:
@mysql_select_db('db_jagguy');
$query = "INSERT INTO tb_one VALUES ('$one','$two','$three')";
$result=mysql_query($query);
if($result){
echo 'Data Saving please wait...';
echo '<meta http-equiv="refresh" content="5">';// Change refresh value
}
ASKER
Not sure what your trying to say as i just asked a small question.
My code works fine. If there is a newer version then I will wait for this but until then this code will work on the host server.
I simply wanted to be on the same page with inserting data and have a comment appear 'data saved' when I click on a submit button.
My code works fine. If there is a newer version then I will wait for this but until then this code will work on the host server.
I simply wanted to be on the same page with inserting data and have a comment appear 'data saved' when I click on a submit button.
I am posting the same code as you want. See above I have only added 3 lines new in your code.
if($result){
echo 'Data Saving please wait...';
echo '<meta http-equiv="refresh" content="5">';// Change refresh value
}
ASKER
You have lost me a bit.
This works fine so what does this other code above do?
This works fine so what does this other code above do?
<?php
$one=$_POST['one'];
$two=$_POST['two'];
$three=$_POST['three'];
//$link = mysql_connect('localhost', 'jagguy', 'mexican');
$link = mysql_connect('localhost', 'root');
//$link=mysql_connect("localhost", "jagguy", "password") or die(mysql_error());
@mysql_select_db('db_jagguy');
$query = "INSERT INTO tb_one VALUES ('$one','$two','$three')";
mysql_query($query);
mysql_close();
echo header("Location: http://127.0.0.1/phptest/displayForm.php");
exit();
?>
Your code should be like:
<form action="addData2.php" method="post">
Value1: <input type="text" name="one"><br>
Value2: <input type="text" name="two"><br>
Value3: <input type="text" name="three"><br>
<input type="Submit">
</form>
<?php
$one=$_POST['one'];
$two=$_POST['two'];
$three=$_POST['three'];
//$link = mysql_connect('localhost', 'jagguy', 'mexican');
$link = mysql_connect('localhost', 'root');
//$link=mysql_connect("localhost", "jagguy", "password") or die(mysql_error());
@mysql_select_db('db_jagguy');
$query = "INSERT INTO tb_one VALUES ('$one','$two','$three')";
if($result){
echo 'Data Saving please wait...';
echo '<meta http-equiv="refresh" content="5">';// Change refresh value
}
?>
ASKER
ok how does this work as it worked but I dont understand it.
$result has no value to attach?
How does this file redirect to the file that called it?
echo '<meta http-equiv="refresh" content="5">';// Change refresh value
$result has no value to attach?
How does this file redirect to the file that called it?
echo '<meta http-equiv="refresh" content="5">';// Change refresh value
jagguy, you code as posted should not be put on the internet. There is no filtering of inputs and using 'root' makes it possible for an SQL Injection attack to delete every database on that server.
Oh sorry I forget the line of mysql_query
see here:
see here:
<form action="addData2.php" method="post">
Value1: <input type="text" name="one"><br>
Value2: <input type="text" name="two"><br>
Value3: <input type="text" name="three"><br>
<input type="Submit">
</form>
<?php
$one=$_POST['one'];
$two=$_POST['two'];
$three=$_POST['three'];
//$link = mysql_connect('localhost', 'jagguy', 'mexican');
$link = mysql_connect('localhost', 'root');
//$link=mysql_connect("localhost", "jagguy", "password") or die(mysql_error());
@mysql_select_db('db_jagguy');
$query = "INSERT INTO tb_one VALUES ('$one','$two','$three')";
$result=mysql_query($query);
if($result){
echo 'Data Saving please wait...';
echo '<meta http-equiv="refresh" content="5">';// Change refresh value
}
?>
I think you understand rest of the code here i assign mysql_query result to $result variable and if($result) then I show data saving please wait... and the meta tag refresh refresh the page and content="5" means pause time for the page increase and reduce the pause time you will see effect if you want immediate keep 0 there.
I think you should take DaveBaldwin's comments pretty seriously here. ID: 39270394
some references for sql injection protection:
http://php.net/manual/en/security.database.sql-injection.php
http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php
http://www.veracode.com/security/sql-injection
http://www.homeandlearn.co.uk/php/php13p5.html
some references for sql injection protection:
http://php.net/manual/en/security.database.sql-injection.php
http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php
http://www.veracode.com/security/sql-injection
http://www.homeandlearn.co.uk/php/php13p5.html
ASKER
The password isnt set on my local machine as it wont work with password I set (see my other post).
This isnt an issue as an uploaded a site will have a password and as for checking inputs again this is a test program.
I want to be able to tell if the data has been saved so is there a way to activate a 'data saved message'. maybe just use jquery to detect a button press?
This isnt an issue as an uploaded a site will have a password and as for checking inputs again this is a test program.
I want to be able to tell if the data has been saved so is there a way to activate a 'data saved message'. maybe just use jquery to detect a button press?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It's often hard to tell the level of knowledge here at EE, so please forgive me if I am being too elementary.
You need to get a bit of a foundation in how all of this stuff works. This article gives some ideas and suggests some good learning resources.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
You need to understand the HTTP client/server protocol.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html
You need to understand how PHP works when dealing with forms. Follow the introductory tutorial here.
http://php.net/tut.php
In the HTML form tag, the action= attribute designates the URL of the script to be started when the form is submitted. If you omit this attribute, the script will make its "submit" request to its own URL. You can detect the presence of request data in $_POST and make your script work in any way that makes sense for your application needs. Try this example.
http://www.laprbass.com/RAY_temp_jagguy.php
PS: Get off MySQL as soon as possible.
You need to get a bit of a foundation in how all of this stuff works. This article gives some ideas and suggests some good learning resources.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
You need to understand the HTTP client/server protocol.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html
You need to understand how PHP works when dealing with forms. Follow the introductory tutorial here.
http://php.net/tut.php
In the HTML form tag, the action= attribute designates the URL of the script to be started when the form is submitted. If you omit this attribute, the script will make its "submit" request to its own URL. You can detect the presence of request data in $_POST and make your script work in any way that makes sense for your application needs. Try this example.
http://www.laprbass.com/RAY_temp_jagguy.php
<?php // RAY_temp_jagguy.php
error_reporting(E_ALL);
// DEMONSTRATE HOW TO USE A POST REQUEST TO THE SAME URL
// IF THERE IS A POST-METHOD REQUEST
if (!empty($_POST))
{
// PRINT OUT THE REQUEST VARIABLES
var_dump($_POST);
// DO ANY OTHER PROCESSING THAT MAKES SENSE HERE
echo "THANK YOU";
}
// CREATE THE HTML FOR BY USING HEREDOC NOTATION
$form = <<<EOD
<form method="post">
Value1: <input type="text" name="one"><br>
Value2: <input type="text" name="two"><br>
Value3: <input type="text" name="three"><br>
<input type="Submit">
</form>
EOD;
// WRITE THE FORM TO THE BROWSER
echo $form;
HTH, ~RayPS: Get off MySQL as soon as possible.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
Not sure where to go next. ?