I'm looking for the simplest way to use the POST method for passing parameters from javascript/jquery on one page to PHP on another page.
I should mention that both pages are PHP templates for wordpress pages, but I guess it shouldn't matter.
Would appreciate examples for code on both sides.
I'm looking for the simplest way to use the POST method for passing parameters from javascript/jquery on one page to PHP on another page.
I should mention that both pages are PHP templates for wordpress pages, but I guess it shouldn't matter.
Would appreciate examples for code on both sides.
Share asked Sep 19, 2011 at 4:56 AshAsh 1,3093 gold badges17 silver badges25 bronze badges 7- Well, I know how to use ajax to run code on a php page but I don't know how to open a php page and pass it parameters. Can you give an example? – Ash Commented Sep 19, 2011 at 5:06
- Sounds like looking for ajax tutorial. have a look at my answer here stackoverflow./questions/7199797/some-ajax-help/… – Gowri Commented Sep 19, 2011 at 5:11
- OK, I think I got it. Now, what if I don't want an answer from the PHP page, but I want open and stay on it? I wish you wrote an answer instead of a ment so I can give you the credit you deserve. – Ash Commented Sep 19, 2011 at 5:25
- :if you think i helped you.please upvote that answer stackoverflow./questions/7199797/some-ajax-help/… SO not wish to place duplicate answer.Thanks for your appreciation – Gowri Commented Sep 19, 2011 at 5:31
- Will be happy to. Can you please answer the question from my previous ment? – Ash Commented Sep 19, 2011 at 5:39
3 Answers
Reset to default 3$.ajax({
type: 'POST',
url: 'otherpage.php',
data: $('form').serialize(), //you may want to give id of the form which contains elements you want to POST
success: function(){
//code on success
},
dataType: 'html'
});
edit 1 incase you want to specify your own parameters to send then use:
$.ajax({
type: 'POST',
url: 'otherpage.php',
data: {
paramname1 : "put_param_value_here",
paramname2 : "put_param_value_here",
paramname3 : "put_param_value_here"
},
success: function(){
//code on success
},
dataType: 'html'
});
Edit 2: if you only want to post your own parameters to a page (no ajax. simple POST which will open the page then do the following)
function postToUrl(url, params, newWindow)
{
var form = $('<form>');
form.attr('action', url);
form.attr('method', 'POST');
if(newWindow){ form.attr('target', '_blank'); }
var addParam = function(paramName, paramValue){
var input = $('<input type="hidden">');
input.attr({ 'id': paramName,
'name': paramName,
'value': paramValue });
form.append(input);
};
// Params is an Array.
if(params instanceof Array){
for(var i=0; i<params.length; i++){
addParam(i, params[i]);
}
}
// Params is an Associative array or Object.
if(params instanceof Object){
for(var key in params){
addParam(key, params[key]);
}
}
// Submit the form, then remove it from the page
form.appendTo(document.body);
form.submit();
}
call like this:-
postToUrl('otherpage.php', {paramname1: "value_here", paramname2: "value_here"});
code taken from answer on JavaScript post request like a form submit
you can use query string parameters
$.post("anotherPage.php?param1=one¶m2=two",function(data){
console.log("data"); //prints yahoo
console.log("success");
});
or
$.post("anotherPage.php",{param1:'one',param2:'two'},function(data){
console.log("data"); //prints yahoo
console.log("success");
});
in the php page
$param1=$_POST['param1'];//gives you one
$param2=$_POST['param1'];//gives you two
echo "yahoo";
EDIT
from the ment no JamesSmith answer i think you want to redirect to other page in that case
Page1.php
<script>
location.href='page2.php?param1=one¶m2=two'; // this causes the page to redirect
</script>
page2.php
<h1><?php echo $_GET['param1']; ?></h1>
<h2><?php echo $_GET['param2']; ?></h2>
yet another edit
you can have a form in page1 and post it to page2
<form action="page2.php" method="post">
<input name="param1" type="text" />
<input name="param2" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
in page2.php
<h1><?php echo $_POST['param1']; ?></h1>
<h2><?php echo $_POST['param2']; ?></h2>
you can do what you wish to do in ajax success according to response
$.post('server.php', ({parm:"1"}) function(data) {
if (data == 1){
window.href = "your redirecting location";
}
else
{
//do something
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744915334a4600799.html
评论列表(0条)