Link to home
Start Free TrialLog in
Avatar of jagguy
jagguyFlag for Australia

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.

<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();            
      
 	

?>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Wow, you have a lot of work to do...  You might want to start with the remediation of MySQL.
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. ?
Avatar of Jagadishwor Dulal
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
}  

Open in new window

Avatar of jagguy

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

Open in new window

Avatar of jagguy

ASKER

You have lost me a bit.
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();

?>

Open in new window

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
}  
 	

?>

Open in new window

Avatar of jagguy

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
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:
<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
}  

?>

Open in new window

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.
Avatar of jagguy

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?
ASKER CERTIFIED SOLUTION
Avatar of Jagadishwor Dulal
Jagadishwor Dulal
Flag of Nepal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

<?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;

Open in new window

HTH, ~Ray

PS: Get off MySQL as soon as possible.