Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

redirect insert data

Posted on 2013-06-23
13
Medium Priority
?
815 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 111

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 

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 84

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 49

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 2000 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 111

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

610 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