简体   繁体   English

使用AJAX发送数据并使用PHP进行检索

[英]Sending data using AJAX and retrieve using PHP

I have the following ajax code: 我有以下的ajax代码:

optionScope.data().stage = 'b';
    $.ajax({
        url: "functions/contact.php",
        type: "post",
        data: {'stage': optionScope.data().stage},
        success: function(data, status) {
          console.log("data sent successfully");
          console.log(data);
          console.log(status);

      },
      error: function(xhr, desc, err) {
        console.log(xhr);
        console.log("Details: " + desc + "\nError:" + err);
      }
    }); // end ajax call

How the data is being retrieved 如何检索数据

<?php

      $stage = $_POST['stage'];
      echo $stage;

?>

Problem is i do not see it being echo out. 问题是我看不到它正在被呼出。 You may have notice that i've place a few log to keep track. 您可能已经注意到,我放置了一些日志以进行跟踪。 Well it does pass success, but for console.log(data) , it just prints data and not the actual 'stage' data. 好吧,它确实通过了成功,但是对于console.log(data) ,它仅打印data而不是实际的“ stage”数据。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks in advance. 提前致谢。

The php code is included in the main html page as such: php代码包含在html主页中,如下所示:

<?php include('functions/contact.php'); ?>

the php code is as follow: 的PHP代码如下:

<?php


  // Honey pot trap
  // Create a hidden input that is only visible to bots. If it's empty than proceed.
  if (empty($_POST['humancheck'])){
    // Proceeed if submit button have been pressed

      $fullName = $_POST['fname'];
      $email = $_POST['email'];

      $stage = $_POST['stage'];
      // Responses provided
      echo "<script>alert($stage); </script>";
      // Sanitize input data
[...]

?>

The JS file is included in the main page, and the php code is run once a user has hit on the submit button. JS文件包含在主页中,并且一旦用户单击了“提交”按钮,就会运行php代码。

<form method="post" action="">
          <!-- Setting up the honey pot trap by including a hidden input only accessable by bots -->
          <input type="hidden" name="humancheck" placeholder="enter your title">
          <div class="input_container">

          <span class="input_icon"><i class="fa fa-user"></i></span><input class="inputContact" name="fname" type="text" placeholder="Your Name" required>
        </div>
          <br>

          <div class="input_container">
            <span class="input_icon">
              <i class="fa fa-envelope-o"></i>
            </span>
            <input class="inputContact" name="email" type="email" placeholder="Email" required>
          </div><br><br>

          <button type="button" class="previousfinal"><i class="fa fa-arrow-left"></i>&nbsp; Previous</button>
          &nbsp;
          <button class="final" name="mailSubmit" type="submit"><i class="fa fa-gift"></i>&nbsp; Get My Pack</button>
        </form>

I was trying to figure out what optionScope might look like and here is what I came up with: 我试图弄清楚optionScope可能是什么样子,这是我想到的:

var optionScope= new function() {
    return {
        data: function () {
            return {stage:'defaultValue'}
        }
    }
}

It doesn't seem that you can assign a property of an object returned by a function in the way you are trying: 您似乎无法以尝试的方式分配由函数返回的对象的属性:

optionScope.data().stage = 'b';

console.log(optionScope.data().stage); // defaultValue

Demo Fiddle 演示小提琴

Edit 编辑

Ok, I think I understand where you're going wrong now. 好的,我想我知道您现在要出问题了。

Try this: 尝试这个:

$(function () {                         // wait until dom is ready
    $('form').submit(function (e) {
        e.preventDefault();             // prevent form from submitting
        var data = $(this).serialize(); // get the form data
        data += '&stage=b';             // add stage to the data
        console.log(data)
        $.ajax({
            url: "functions/contact.php",
            type: "post",
            data: data,
            success: function (data, status) {
                console.log("data sent successfully");
                console.log(data);
                console.log(status);

            },
            error: function (xhr, desc, err) {
                console.log(xhr);
                console.log("Details: " + desc + "\nError:" + err);
            }
        });
    });
});

change your code to this 将您的代码更改为此

optionScope.data().stage = 'b';
$.ajax({
    url: "functions/contact.php",
    type: "post",
    data: {stage: optionScope.data().stage}, // don't put quotes around stage
    success: function(data, status) {
      console.log("data sent successfully");
      console.log(data);
      console.log(status);

  },
  error: function(xhr, desc, err) {
    console.log(xhr);
    console.log("Details: " + desc + "\nError:" + err);
  }
}); // end ajax call

you don't need to put quotes around the key for your data 您无需在数据的密钥周围加上引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM