简体   繁体   中英

How to send data to a PHP script and by using the jQuery AJAX load() instruction

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;

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

In Js:

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

In php you can access a post string like below:

$input = $_POST['data'];

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

$( "#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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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