1

I am using Ajax to save textarea conent in database:

$("#update").click(function(e) {
      e.preventDefault();
      var ttle = $("#title").val(); 
      var text = $("#prjdesc").val(); 
     var dataString = 'param='+text+'&param1='+ttle;
      $.ajax({
        type:'POST',
        data:dataString,
        url:'insert.php',
        success:function(data) {
          alert(data);
        }
      });
    });

insert.php:

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

 $name = $_POST['param'];
 $title = $_POST['param1'];
 $sql="INSERT INTO $tbl_name (title, content) VALUES ('$name','$title')";

 if(mysql_query($sql)) {
  echo "Success";
  } else {
  echo "Cannot Insert";
   }

I am alerting success message when data is successfully saved. I need to know how to displayed on another page when the data is successfully saved.

How is this done?

benRollag
  • 1,219
  • 4
  • 16
  • 21
user2380296
  • 13
  • 1
  • 6
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://stackoverflow.com/a/14110189/1723893). – NullPoiиteя May 15 '13 at 06:10
  • do procession on the page you want to show data .. – NullPoiиteя May 15 '13 at 06:12
  • @N̨ul̕L͑P̯͍̭ȏͣ͛iƞer:thank you...i will change it..but my db is mysql..what i need to write instead of this?? – user2380296 May 15 '13 at 06:15
  • @N̨ul̕L͑P̯͍̭ȏͣ͛iƞer:yeah i want show data on click update.. – user2380296 May 15 '13 at 06:16
  • there is no problem with your `Mysql DB`, just follow this link below it will teach you how to convert you `mysql_` code to `PDO`. http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – 6339 May 15 '13 at 06:21

4 Answers4

0

in success() callback function of ajax you can redirect to another page that will displayed the saved data

somewhat like this

$("#update").click(function(e) {
   e.preventDefault();
   var ttle = $("#title").val(); 
   var text = $("#prjdesc").val(); 
   var dataString = 'param='+text+'&param1='+ttle;
       $.ajax({
            type:'POST',
            data:dataString,
            url:'insert.php',
            success:function(data) {
              //here set the another page to redirect to another page
              window.location ="http://localhost/myanotherpage"; 
            }
      });
});
Netorica
  • 18,523
  • 17
  • 73
  • 108
0

You can check for result using response in success call back method of ajax(currently it is a simple string), if that is success, redirect to your desired page.

$("#update").click(function(e) {
   e.preventDefault();
   var ttle = $("#title").val(); 
   var text = $("#prjdesc").val(); 
   var dataString = 'param='+text+'&param1='+ttle;
   $.ajax({
        type:'POST',
        data:dataString,
        url:'insert.php',
        success:function(data) {
          //here set the another page to redirect to another page
         if($.trim(data).toLowerCase()=="success"){
          window.location =$your_page_url; 
          }
        }
      });
});
praveen
  • 194
  • 1
  • 10
  • In your code if it is success then it redirected to another page...where i need to display the 'param' data.... – user2380296 May 15 '13 at 06:32
  • what is primary key of your table?,you can get the primary key value in response, after that you can use that key in getting desired data from database by using url like "your_page_url/$current_inserted_row_id" – praveen May 15 '13 at 06:42
  • i have id as the primary key in my table – user2380296 May 15 '13 at 07:27
0
  $("#update").click(function(e) {
          e.preventDefault();
          var ttle = $("#title").val(); 
          var text = $("#prjdesc").val(); 
          var dataString = 'param='+text+'&param1='+ttle;
  $.ajax({
         type:'POST',
         data:dataString,
         url:'insert.php',
         success:function(data) {
         alert(data);
         window.location='another.php';
    }
  });
});

step 1 ) write window.location='another.php'; in jquery after getting success as I mentioned above (This will redirect you to another.php page)
Step 2 ) create another.php page and write select query to fetch data and display result on it

J.Rob
  • 436
  • 1
  • 5
  • 22
  • i think it will take time get the data from db..i want on the spot.whenever i click update button it has to save and display immediately – user2380296 May 15 '13 at 07:20
0

check this out,

$("#update").click(function(e) {
      e.preventDefault();
      var ttle = $("#title").val(); 
      var text = $("#prjdesc").val(); 
     var dataString = 'param='+text+'&param1='+ttle;
      $.ajax({
        type:'POST',
        data:dataString,
        url:'insert.php',
        success:function(id) {
          window.location ="path.php?id="+id; // redirects
        }
      });
    });

insert.php

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

 $name = $_POST['param'];
 $title = $_POST['param1'];
 $sql="INSERT INTO $tbl_name (title, content) VALUES ('$name','$title')";

 if(mysql_query($sql)) {
  echo mysql_insert_id(); // this will get your last inserted ID
  } else {
  echo "Cannot Insert";
  }
6339
  • 475
  • 3
  • 16
  • r u sure it will work???i have a doubt becoz the param is in var dataString,so we can directry acces with name param???? – user2380296 May 15 '13 at 07:18
  • @user2380296 in this case `param` is just a variable. k i'll change to another name. and try this. – 6339 May 15 '13 at 07:25
  • @user2380296 I hope you have an `auto-increment` field in the table. – 6339 May 15 '13 at 07:27
  • yup i have auto-increment for id – user2380296 May 15 '13 at 07:29
  • here we are passing id as a parameter but it will take the null value as id,becoz which is not defined... – user2380296 May 15 '13 at 07:36
  • @user2380296 the id which is defined in `insert.php` that is, `echo mysql_insert_id();` It will return your last inserted ID and pass through `id` parameter of `success` function. – 6339 May 15 '13 at 09:02
  • how to access that id in path.php – user2380296 May 15 '13 at 09:46
  • @user2380296 `echo mysql_insert_id();` on your `insert.php` will do the task. if it returns nothing the the `id` will be null/empty. – 6339 May 15 '13 at 09:50
  • $temp=$_GET['id']; $query = mysql_query("SELECT content FROM project_details WHERE id=". $temp); how to display this $query??? – user2380296 May 16 '13 at 06:31
  • `if (!$query) { echo 'Could not run query: ' . mysql_error(); exit; } $row = mysql_fetch_row($query); echo $row[0]; // content` – 6339 May 16 '13 at 06:55