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:

  1. tells browser navigate b.php (using request)
  2. 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:

  1. don't navigate different page (remove line using `location)
  2. 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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -