【问题标题】:using AJAX to call one query from a PHP file which has multiple使用 AJAX 从一个 PHP 文件中调用一个查询,该文件有多个
【发布时间】:2015-05-03 01:50:17
【问题描述】:

我想使用 AJAX 在一个有多个的 php 文件中调用单个查询,但我不完全确定如何去做。

本质上,php 文件将包含三个 PDO 查询,即更新、删除和添加。目前它只有更新查询。 我如何向文件中添加第二个查询并让 AJAX 调用特定查询?

AJAX 代码:

function updateCall() {
  var data = $('#updateForm').serialize();
  $.post('ManageCourses_DeleteSubmit.php', data, function(response){

    $("#updateForm").html(response);
    //'soft'reload parent page, after a delay to show message
    setTimeout(function(){
      $('#editModal').modal('hide')
      location.reload();
    },2000);

  }).fail(function(jqXHR, textStatus) {
    alert( "Request failed: " + textStatus );
  });
}

php 文件:

<?php

include "db_conx.php";

try
{
    $db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);

    $db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = $db_conx->prepare("UPDATE course_details SET course_title = :course_title 
        WHERE course_code = :course_code");

    $course_title = $_POST['course_title'];
    $course_code = $_POST['course_code'];

    $sql->bindParam(':course_title', $course_title, PDO::PARAM_STR);
    $sql->bindParam(':course_code', $course_code, PDO::PARAM_STR);


    /*** execute the prepared statement ***/
    $sql->execute();

    /*** success message ***/


    $message = "<p class='text-success'> Record Successfully Updated <span class='glyphicon glyphicon-ok'/></p>";
} 
catch(Exception $e)
{
    $message = 'Message: ' .$e->getMessage();
}

die($message);
?>

有什么例子吗?

谢谢!

【问题讨论】:

标签: mysql ajax pdo


【解决方案1】:

$.ajax() 代码块如下所示:

$.ajax({
    type: "POST",
    url: "receiving_file.php",
    data: 'selected_opt=' + opt + '&something_else=' +someelse+'&more_stuff='+more_stuff,
    success:function(data){
        alert('This was sent back: ' + data);
    }
});

注意data:

只需使用另一个变量来确定您将调用哪个 PHP 例程。比如:

data: 'myrequest=add&selected_opt=' + opt + '&something_else=' +someelse+'&more_stuff='+more_stuff,

对比

data: 'myrequest=delete&selected_opt=' + opt + '&something_else=' +someelse+'&more_stuff='+more_stuff,

然后,在您的 PHP 文件中,测试该变量:

<?php
$req = $_POST['myrequest'];
if ($req == 'add'){
    //do the add
}else if ($req == 'delete'){
    //etc
}

或者,您可以使用单个 $.ajax() 代码块,并使用变量来确定要调用哪个 PHP 函数:

if (useraction=='add') {
    myreq = 'add';
}else if(useraction=='del') {
    myreq = 'delete';
}

//later on in the code...

$.ajax({
    type: "POST",
    url: "receiving_file.php",
    data: 'myrequest=' +myreq+ '&selected_opt=' + opt + '&something_else=' +someelse+'&more_stuff='+more_stuff,
    success:function(data){
        alert('This was sent back: ' + data);
    }
});

【讨论】:

  • 我将创建三个单独的 ajax 调用 - 一个用于更新,一个用于删除,一个用于添加。然后希望代码中的某些内容能够区分执行哪个查询
  • 是的,您的 javascript/jQuery 代码中有三个 $.ajax() 代码块,但每个代码块都有不同的 myrequest=____ 参数。在 PHP 端,您可以看到该变量的内容。
  • @user90210 如果您想查看更多简化的 AJAX 示例,see this post
  • 谢谢,请原谅我的幼稚,但我对“myrequest=____ 参数”如何连接到 php 文件感到困惑。如何在 php 中创建多个查询并获取 ajax 来区分它们?
  • 哦,我看到了您评论中的最后一个代码位。对不起! :$
猜你喜欢
  • 1970-01-01
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多