AJAX POST组的PHP表单变量 [英] AJAX POST group of form variables to PHP

查看:96
本文介绍了AJAX POST组的PHP表单变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一组表单参数发送到PHP脚本进行处理.

I am trying to send a group of form parameters over to a PHP script for processing.

我以前曾经使用$.post做过这样的事情,但是现在我试图通过使用$.ajax来严格完成它.

I've previously done something like this using $.post, but now I'm trying to get it done strictly by using $.ajax.

这是应该将所有变量发送到PHP脚本的jQuery click事件:

Here is the jQuery click event that is supposed to send all of the variables to the PHP script:

$('.searchSubmit').on('click', function()
{
  var searchCriteria = {
      import_bill: $('#import_bill').val(), 
      import_ramp: $('#import_ramp').val(), 
      import_delivery: $('#import_delivery').val(), 
      // few more form parameters
    };

  $.ajax({
    url: 'api/railmbs.php', // process script
    type: 'POST',
    data: searchCriteria, // parameter group above
    dataType: 'html' // had this set to json, but only got fail 
    success: function(data, textStatus, jqXHR)
    {
      console.log(data);
    },
    error: function(jqHHR, textStatus, errorThrown)
    {
      console.log('fail');
    }
  });
});

这是一个名为railmbs.php的PHP脚本:

Here is the PHP script, called railmbs.php:

<?php
  if(isset($_POST['searchCriteria']))
  {
    $value = $_POST['searchCriteria'];
    $_SESSION['where'] = "";

    $import_bill = mysqli_real_escape_string($dbc, trim($value['import_bill']));
    $import_ramp = mysqli_real_escape_string($dbc, trim($value['import_ramp']));
    $import_delivery = mysqli_real_escape_string($dbc, trim($value['import_delivery']));

    echo $import_bill; // just trying to echo anything at this point
  }
?>

不确定我在做什么错.如果在上述IF之前输入echo hello,则控制台将相应输出.但是我似乎无法从IF内部获得任何东西给echo.

Not sure what I am doing wrong. If I echo hello before the IF above, the console will output accordingly. But I cannot seem to get anything to echo from inside the IF.

有人看到我的错误吗?

推荐答案

您没有设置"searchCriteria"变量.

You are not setting the "searchCriteria" variable.

更改此:

$('.searchSubmit').on('click', function()
{
  var searchCriteria = {
      import_bill: $('#import_bill').val(), 
      import_ramp: $('#import_ramp').val(), 
      import_delivery: $('#import_delivery').val(), 
      // few more form parameters
    };

  $.ajax({
    url: 'api/railmbs.php', // process script
    type: 'POST',
    data: searchCriteria, // parameter group above
    dataType: 'html' // had this set to json, but only got fail 
    success: function(data, textStatus, jqXHR)
    {
      console.log(data);
    },
    error: function(jqHHR, textStatus, errorThrown)
    {
      console.log('fail');
    }
  });
});

收件人:

$('.searchSubmit').on('click', function()
{
    var data = {

    searchCriteria: {

      import_bill: $('#import_bill').val(), 
      import_ramp: $('#import_ramp').val(), 
      import_delivery: $('#import_delivery').val(), 
      // few more form parameters

     } 

    };

  $.ajax({
    url: 'api/railmbs.php', // process script
    type: 'POST',
    data: data, // parameter group above
    dataType: 'html' // had this set to json, but only got fail 
    success: function(data, textStatus, jqXHR)
    {
      console.log(data);
    },
    error: function(jqHHR, textStatus, errorThrown)
    {
      console.log('fail');
    }
  });

这篇关于AJAX POST组的PHP表单变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆