2

I got a jQuery load function that works like this:

function admin_loadPage(page) {
    history.pushState(null, null, '?page='+page);
    $('#page').html('<center><img src="../includes/images/loader.gif" /></center>');
    $('#page').load('content/'+page+'.php');
}

How would i sent data to my content/+page+.php whatever the page i want to load, and how do i recive the data on the page itself?

1
  • What kind of transport? POST? GET? for GET, just add ?name=value to the url. for POST, you'll have to pass an object as the second parameter. Commented Mar 19, 2013 at 19:53

3 Answers 3

3

you can send data by sending object in second parameter and getting response in third callback function

$('#page').load('content/'+page+'.php', {data1: 'yourdata', data2: 'otherdata'},function(result){
    alert(result);
})

page.php

you can get send data by post

 $data=$_POST['data1'];  //will give you yourdata
 $anotherdata=$_POST['data2']; //will give you otherdata 
 echo "test";

here you are printing test as response so the alert in post should give you an alert with test in it...

Sign up to request clarification or add additional context in comments.

Comments

1

To send data simply put a javascript object containing your data in the second parameter of the load method.

$('#page').load('content/'+page+'.php', {param1: 'SomeData', param2: 'SomeOtherData'})

I'm not exactly sure what you mean by "how do i recive the data on the page itself?" In this example the load method will append the content returned from the server to the HTML element with id "page". Is that not what you are looking to do?

4 Comments

you have no callback... this answers only half of the question
If we say that "page" = users, then in my content/users.php how where I to get the data from param1, param2 ect?
sounds like a php question that has been mislabeled tbh. but in php to access variables sent via ajax you would use something like $param1 = $_POST['param1'] $param2 = $_POST['param2'] etc... you will have to use key value pairs in your javascript object and the keys must match up with whatever you access them with in php
Yes, I think you're right iAmClownShoe. Thanks for putting the php specifics. Not my strong suit.
1

you should be using ajax to do this. if i understand you correctly, you would like to send date to the server and receive data that you will then show on the page? well to do this, as i mentioned, ajax is your best friend. http://api.jquery.com/jQuery.ajax/

using ajax you can send a javascript object to the server(typically in JSON format) which will process it and return some value to you. the jquery ajax method also has a callback function that you can use to then display whatever data was returned to you. As we do not see your server side code this is difficult for us to help with but that's the general idea.

4 Comments

Sorry, im lost when it comes to any type of javascript, weather thats ajax, jquery or just normal...
so really the issue isn't that you can't figure out how to do this but that you don't even know where to start... codecademy.com
i dont mean to sound like a dick but if you don't even know what javascript, ajax, and jquery are, then you probably won't get much further even if we do give you the answer written out.
but basically ajax allows you to asynchronously (if you don't know what this means look it up) send data to your webserver. the jquery ajax method takes a bunch of properties that you can assign how you want ie: url, data, dataType, success, error etc. the url is the path to your server side script, data is the object you are sending it (json formatted), dataType is the data type you are expecting in return, success will fire a defined function upon receiving a success response that will return data, xhrstatus, and textstatus, and same with error for error responses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.