简体   繁体   English

通过JQuery $ .post将JavaScript数组传递给PHP

[英]Passing a JavaScript Array To PHP Through JQuery $.post

This piece of code gets all the form variables and sends them via AJAX to the PHP script. 这段代码获取所有表单变量,并通过AJAX将其发送到PHP脚本。 But I want the calculated results from the PHP script that is being returned to the javascript via a JSON encoded array to be in the form of "post":{"uname":"someNamefromForm","email":"someEmail","fname":"namefromtheform","lname":"lastnamefromform" }... The output I'm getting now is "uname=e&email=e&fname=e&lname=euname".. This is the JSON array I want to displayed at the bottom of the page for debugging purposes. 但我希望通过JSON编码数组返回到javascript的PHP脚本的计算结果的格式为“ post”:{“ uname”:“ someNamefromForm”,“ email”:“ someEmail”,“ fname“:” namefromtheform“,” lname“:” lastnamefromform“} ...我现在得到的输出是” uname = e&email = e&fname = e&lname = euname“。。这是我想在其中显示的JSON数组页面底部用于调试目的。 Can someone tell me how to format it please 有人可以告诉我如何格式化吗

This is my HTML form 这是我的HTML表格

  <div id="wrapper">
      <h2> Validation with AJAX,JQuery,JSON and PHP</h2>
          <div class="form-container">    
              <span id="ajax-message"></span>

              <form id="ajax-form" onsubmit="return false;">
                  <p class="legend">All fields marked with an asterisk are required.</p>

                  <fieldset>
                      <legend>User Details</legend>
                      <div>
                        <label for="uname">Username <em>*</em></label> 
                        <input id="uname" type="text" name="uname" value=""  />
                      </div>
                      <div>
                        <label for="email">Email Address <em>*</em></label> 
                        <input id="email" type="text" name="email" value="" />
                      </div>
                      <div>
                        <label for="fname" class="error">First Name <em>*</em></label> 
                        <input id="fname" type="text" name="fname" value="" size="50" class="error"/>
                        <p class='note'>All error message go here </p> 
                      </div>
                      <div>
                        <label for="lname">Last Name <em>*</em></label> 
                        <input id="lname" type="text" name="lname" value="" size="50" />
                      </div>
                  </fieldset>

                  <div class="buttonrow">
                      <input type="submit" value="Submit This Form via AJAX" class="button" />  
                      <input type="button" value="Start Again" class="button" />
                      <a >Refresh this Page</a>
                  </div>
              </form>
          </div> 
      <h3>JSON Array</h3>
      <pre id='debug'></pre>
  </div>

This is my javascript 这是我的JavaScript

$("#ajax-form").submit(function(){
          var variableToSend = $(this).serialize();
          $.post(
            'ajaxformval_post.php', 
            {variable: variableToSend}, 
            function(data){$("#debug").html(data)},
            "json"
          );
      })

This is the php 这是PHP

<?php
    $variable = $_POST['variable'];

    echo json_encode($variable);

    /*$json = array(
        'booleanFlag' => TRUE,
        'someText' => "AJAX should be renamed AJAJ",
        'anArrayOfData' => array('name' => 'Mickey', 'ears' => 'very Big') 
    );*/

 ?>

You can send your serialized variable directly like this: 您可以像这样直接发送序列化变量:

$("#ajax-form").submit(function(){
      var variableToSend = $(this).serialize();
      $.post(
        'ajaxformval_post.php', 
         variableToSend, 
        function(data){$("#debug").html(data)},
        "json"
      );
  });

Then on the server side simply output the json_encoded post: 然后在服务器端只需输出json_encoded帖子:

<?php
    $variable = $_POST;

    echo json_encode($variable);
 ?>

Did you try changing the "json"-parameter from the $.post-method? 您是否尝试过通过$ .post方法更改“ json”参数?

According to documentation https://api.jquery.com/jQuery.post/ 根据文档https://api.jquery.com/jQuery.post/

dataType
Type: String
The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).

So I guess if you give him "json" he will automatically decode the returned data. 因此,我想如果您给他“ json”,他将自动解码返回的数据。

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

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