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?

4

4 回答 4

0

success()ajax 的回调函数中,您可以重定向到另一个页面,该页面将显示保存的数据

有点像这样

$("#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"; 
            }
      });
});
于 2013-05-15T06:14:37.187 回答
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';
    }
  });
});

步骤 1 ) 写 window.location='another.php'; 在我上面提到的成功后在 jquery 中(这会将您重定向到 another.php 页面)
步骤 2)创建 another.php 页面并编写选择查询以获取数据并在其上显示结果

于 2013-05-15T06:41:33.647 回答
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(id) {
          window.location ="path.php?id="+id; // redirects
        }
      });
    });

插入.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";
  }
于 2013-05-15T06:53:50.667 回答
0

您可以使用ajax的成功回调方法中的响应来检查结果(目前它是一个简单的字符串),如果成功,则重定向到您想要的页面。

$("#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; 
          }
        }
      });
});
于 2013-05-15T06:28:51.343 回答