ajaxjquery<;a>;更新数据库

ajaxjquery<;a>;更新数据库,ajax,jquery,Ajax,Jquery,我有一个视图\u mine\u和\u others.php文件来输出这个html <div class="rec" id="1"> <div class="val">343</div> <a class="change">Change</a> </div> <div class="rec" id="2"> <div class="val">12001</div> <a cla

我有一个
视图\u mine\u和\u others.php
文件来输出这个html

<div class="rec" id="1">
 <div class="val">343</div>
 <a class="change">Change</a>
</div>
<div class="rec" id="2">
 <div class="val">12001</div>
 <a class="change">Change</a>
</div>
<div class="rec" id="3">
 <div class="val">4233</div>
 <a class="change">Change</a>
</div>
update.php
将检查用户是否有权进行此更改,如果有,将进行更改。它还将根据成功指示器设置
$success=0或1
。这个
$success
没有以任何方式返回,因为它不是一个函数,它只是我在更新脚本中的一个内部变量,可以在需要时使用

  • 我如何进行ajax调用,以便获得关于是否允许更新的反馈(update.php中的
    $success=1
    )或(update.php中的
    success=0
    )。我将使用该反馈以不同的方式更新界面
额外信息:

function change() {
   var id = $(this).parent().attr('id'); 
   var val = $(this).siblings('.val').html();

   $.post('/view_mine_and_others.php', { id: id, val: val }, function(data) {
      if (data == '1') {
         alert('success');
      } else {
         alert('error');
      }
   });
   // you MUST return false or the A button will still fire the click event
   return false;
}
// assumes both id and val are integer values (sanitizes data)
$id   = (isset($_POST["id"])) ? (int) $_POST['id'] : null;
$val  = (isset($_POST['val'])) ? (int) $_POST['val'] : null;

// do your database work here
// and assume that you create a variable
// called $success which is either true or false
// depending on if the update was successful

// echo the response to be picked up by the AJAX request
echo ($success) ? 1 : 0;
die;
  • 请注意,我选择不使用表单:)只使用

  • update.php脚本需要一个
    $\u POST['id']
    $\u POST[val]
    (我不打算在这里发布update.php的代码,因为这不是重点,我相信您可以想象它的外观和功能)


将该成功变量的值作为Ajax脚本的输出,并检查Ajax回调中的值。

将该成功变量的值作为Ajax脚本的输出,并检查Ajax回调中的值。

与Karim79类似

我的做法如下:

Javascript

function change(){
   var id = $(this).parent().attr('id'); 
   var val = $(this).siblings('.val').html();

   $.ajax({
       url: 'view_mine_and_others.php',
       dataType:"json",
       type: 'POST',
       data: 'id=' + id + '&val=' + val,
       success: function(resp) {
           if($.isFunction(resp.successFunction)) {
               successFunction.apply();
           } else if($.isFunction(resp.failureFunction)){
               failureFunction.apply();
           }
       }
   });
}
function updateSuccessful(){
  //do something because the server returned a success
}

function updateFailed(){
  //do something else because the server returned a failure
}
PHP

$id=$_POST["id"];
$val=$_POST["val"];
//Do something with these variables
if($success==1){
  echo '{successFunction:function(){
      updateSuccessful();
        }}';
}else{
  echo '{successFunction:function(){
      updateFailed();
        }}';
}
然后再次使用javascript

function change(){
   var id = $(this).parent().attr('id'); 
   var val = $(this).siblings('.val').html();

   $.ajax({
       url: 'view_mine_and_others.php',
       dataType:"json",
       type: 'POST',
       data: 'id=' + id + '&val=' + val,
       success: function(resp) {
           if($.isFunction(resp.successFunction)) {
               successFunction.apply();
           } else if($.isFunction(resp.failureFunction)){
               failureFunction.apply();
           }
       }
   });
}
function updateSuccessful(){
  //do something because the server returned a success
}

function updateFailed(){
  //do something else because the server returned a failure
}
当然,您不需要javascript中额外的“updateSuccessful”和“updateFailed”函数, 因为,您可以在PHP返回的函数中封装这些函数所做的任何事情

因此,不要在PHP中执行此操作

echo '{successFunction:function(){
          updateSuccessful();
      }}';
你能行

echo '{successFunction:function(){
    //do something because the operation was successful
      }}';

PHP代码中的一种javascript代码……

与Karim79类似

我的做法如下:

Javascript

function change(){
   var id = $(this).parent().attr('id'); 
   var val = $(this).siblings('.val').html();

   $.ajax({
       url: 'view_mine_and_others.php',
       dataType:"json",
       type: 'POST',
       data: 'id=' + id + '&val=' + val,
       success: function(resp) {
           if($.isFunction(resp.successFunction)) {
               successFunction.apply();
           } else if($.isFunction(resp.failureFunction)){
               failureFunction.apply();
           }
       }
   });
}
function updateSuccessful(){
  //do something because the server returned a success
}

function updateFailed(){
  //do something else because the server returned a failure
}
PHP

$id=$_POST["id"];
$val=$_POST["val"];
//Do something with these variables
if($success==1){
  echo '{successFunction:function(){
      updateSuccessful();
        }}';
}else{
  echo '{successFunction:function(){
      updateFailed();
        }}';
}
然后再次使用javascript

function change(){
   var id = $(this).parent().attr('id'); 
   var val = $(this).siblings('.val').html();

   $.ajax({
       url: 'view_mine_and_others.php',
       dataType:"json",
       type: 'POST',
       data: 'id=' + id + '&val=' + val,
       success: function(resp) {
           if($.isFunction(resp.successFunction)) {
               successFunction.apply();
           } else if($.isFunction(resp.failureFunction)){
               failureFunction.apply();
           }
       }
   });
}
function updateSuccessful(){
  //do something because the server returned a success
}

function updateFailed(){
  //do something else because the server returned a failure
}
当然,您不需要javascript中额外的“updateSuccessful”和“updateFailed”函数, 因为,您可以在PHP返回的函数中封装这些函数所做的任何事情

因此,不要在PHP中执行此操作

echo '{successFunction:function(){
          updateSuccessful();
      }}';
你能行

echo '{successFunction:function(){
    //do something because the operation was successful
      }}';

PHP代码中的一种javascript代码…

下面是一个简要说明,因为上面的解决方案似乎遗漏了几个步骤,而这段代码处理的是对$\u POST数据的清理,并且可能更容易遵循:

JAVASCRIPT:

function change() {
   var id = $(this).parent().attr('id'); 
   var val = $(this).siblings('.val').html();

   $.post('/view_mine_and_others.php', { id: id, val: val }, function(data) {
      if (data == '1') {
         alert('success');
      } else {
         alert('error');
      }
   });
   // you MUST return false or the A button will still fire the click event
   return false;
}
// assumes both id and val are integer values (sanitizes data)
$id   = (isset($_POST["id"])) ? (int) $_POST['id'] : null;
$val  = (isset($_POST['val'])) ? (int) $_POST['val'] : null;

// do your database work here
// and assume that you create a variable
// called $success which is either true or false
// depending on if the update was successful

// echo the response to be picked up by the AJAX request
echo ($success) ? 1 : 0;
die;
PHP:

function change() {
   var id = $(this).parent().attr('id'); 
   var val = $(this).siblings('.val').html();

   $.post('/view_mine_and_others.php', { id: id, val: val }, function(data) {
      if (data == '1') {
         alert('success');
      } else {
         alert('error');
      }
   });
   // you MUST return false or the A button will still fire the click event
   return false;
}
// assumes both id and val are integer values (sanitizes data)
$id   = (isset($_POST["id"])) ? (int) $_POST['id'] : null;
$val  = (isset($_POST['val'])) ? (int) $_POST['val'] : null;

// do your database work here
// and assume that you create a variable
// called $success which is either true or false
// depending on if the update was successful

// echo the response to be picked up by the AJAX request
echo ($success) ? 1 : 0;
die;

下面是一个简短的概述,因为上面的解决方案似乎遗漏了几个步骤,而这段代码可以处理$\u POST数据的清理,并且可能更容易遵循:

JAVASCRIPT:

function change() {
   var id = $(this).parent().attr('id'); 
   var val = $(this).siblings('.val').html();

   $.post('/view_mine_and_others.php', { id: id, val: val }, function(data) {
      if (data == '1') {
         alert('success');
      } else {
         alert('error');
      }
   });
   // you MUST return false or the A button will still fire the click event
   return false;
}
// assumes both id and val are integer values (sanitizes data)
$id   = (isset($_POST["id"])) ? (int) $_POST['id'] : null;
$val  = (isset($_POST['val'])) ? (int) $_POST['val'] : null;

// do your database work here
// and assume that you create a variable
// called $success which is either true or false
// depending on if the update was successful

// echo the response to be picked up by the AJAX request
echo ($success) ? 1 : 0;
die;
PHP:

function change() {
   var id = $(this).parent().attr('id'); 
   var val = $(this).siblings('.val').html();

   $.post('/view_mine_and_others.php', { id: id, val: val }, function(data) {
      if (data == '1') {
         alert('success');
      } else {
         alert('error');
      }
   });
   // you MUST return false or the A button will still fire the click event
   return false;
}
// assumes both id and val are integer values (sanitizes data)
$id   = (isset($_POST["id"])) ? (int) $_POST['id'] : null;
$val  = (isset($_POST['val'])) ? (int) $_POST['val'] : null;

// do your database work here
// and assume that you create a variable
// called $success which is either true or false
// depending on if the update was successful

// echo the response to be picked up by the AJAX request
echo ($success) ? 1 : 0;
die;

我也是这么想的,但我对ajax真的是个新手。我该如何设置回调等+1来证实我的怀疑。我也是这么想的,但我对ajax真的很陌生。我将如何设置回调等+1来确认我的怀疑。