javascript - passing data using post array in java-script -
i try load b.php a.php after execution in function , pass data using post array a.php b.php within same time.
code list follows a.php
<script type="text/javascript"> alert_for_the_fucntion(); window.location.href = "b.php"; function alert_for_the_fucntion() { $.post("b.php", {action: 'test'}); } </script>
b.php
<?php if (array_key_exists("action", $_post)) { if ($_post['action'] == 'test') { echo 'ok'; } } ?>
for testing purpose tried echo in b.php. not working. have done mistakes? or there possible method this.
your code this:
- tells browser navigate b.php (using request)
- triggers post request using xmlhttprequest
the post request probably gets canceled because browser leaves page (and xhr request asynchronous). if doesn't, response ignored. either way, has no effect.
you see result of request (which, obviously, doesn't include $_post['action']
) displayed in browser window.
if want programmatically generate post request , display result new page need submit form.
don't use location
. don't use xmlhttprequest
(or wraps around it, $.ajax
).
var f = document.createelement("form"); f.method = "post"; f.action = "b.php"; var = document.createelement("input"); i.type = "hidden"; i.name = "action"; i.value = "test"; f.appendchild(i); document.body.appendchild(f); f.submit();
if want process results in javascript then:
- don't navigate different page (remove line using `location)
- add
done
handler ajax code
e.g.
$.post("b.php", {action: 'test'}).done(process_response); function process_response(data) { document.body.appendchild( document.createtextnode(data) ); }
Comments
Post a Comment