Solved

redirect insert data

Posted on 2013-06-23
13
694 Views
Last Modified: 2013-06-24
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

0
Comment
Question by:jagguy
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39270248
Wow, you have a lot of work to do...  You might want to start with the remediation of MySQL.
http://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. ?
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39270270
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

0
 

Author Comment

by:jagguy
ID: 39270272
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.
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39270276
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

0
 

Author Comment

by:jagguy
ID: 39270285
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

0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39270304
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

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:jagguy
ID: 39270320
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
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39270394
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.
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39270419
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.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39270521
0
 

Author Comment

by:jagguy
ID: 39270939
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?
0
 
LVL 15

Accepted Solution

by:
Jagadishwor Dulal earned 500 total points
ID: 39271158
Try using jquery ajax post create a file for your form and insert script here like:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Post Example </title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){
	var one=$("#one").val();
	var two=$("#two").val();
	var three=$("#three").val();
 $.post('savedata.php', {one: one, two: two, three: three},
function(data){
	$("#message").html(data);
	$("#message").hide();
	$("#message").fadeIn(1500); //Fade in the data given by the insert.php file
	$("#message").fadeOut(3500);
	});
	return false;
	});
});
</script>
</head>
<body>
<div id="message"></div>
<form action="" method="post">
Value1: <input type="text" name="one" id="one"><br>
Value2: <input type="text" name="two" id="two"><br>
Value3: <input type="text" name="three" id="three"><br>
<input type="Submit" id="submit">
</form>
</body>
</html>

Open in new window


Now create file savedata.php to save data which will be like:

<?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...';
}else{
	echo 'Error Inserting data';
}

?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39271199
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.
http://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.
http://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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now