如何将数据发送到PHP脚本并使用jQuery AJAX load()指令

如何将数据发送到PHP脚本并使用jQuery AJAX load()指令

问题描述:

I want to commit the string "123456" to the aaa.php script with the jQuery load() function. The general syntax of the load() function looks like this:

$(selector).load(url,data,function(response,status,xhr))

In my case I use the function in the following way

$("#htmlElement").load("scripts/aaa.php",'123456');

How can I get access to the string "123456" inside the following php script?

<?php
  $input; //this should be the string "123456"
  //some calculations
  echo result;

我想将字符串“123456” code>提交到 aaa.php code>脚本与jQuery load() code>函数。 load()函数的一般语法如下所示: p>

  $(selector).load(url,data,function(response,status,xhr))
   pre> 
 
 

在我的情况下,我按以下方式使用该函数 p>

  $(“#htmlElement”)。load(“  scripts / aaa.php“,'123456'); 
  code>  pre> 
 
 

如何访问以下字符串”123456“ code> php脚本? p>

 &lt;?php 
 $ input;  //这应该是字符串“123456”
 //一些计算
 echo结果; 
  code>  pre> 
  div>

In order to use .load() to send data you would have to enclose the data properly:

 $("#htmlElement").load("scripts/aaa.php",{data:'123456'});

Once done the value will be available in the POST array:

$input = $_POST['data']; // will be 123456

You can assign '123456' to an element, and then call it back in the php e.g.

$( "#new-projects" ).load( "/resources/load.html #projects li" );

Have a read of this, it explained the load function perfectly allowing you to find your answer - http://api.jquery.com/load/

In Js:

$("#htmlElement").load("scripts/aaa.php",{data:'123456'});

In php you can access a post string like below:

$input = $_POST['data'];