简体   繁体   中英

Passing values across two php files and loading data using JavaScript

I am trying to solve an existing problem - I have two files a. php and b. php and I am passing one value using HTTP get to b. php. Inside b. php I have following JavaScript code to get the HTTP get value. And by using the following code I am able to get the value. Then, I am trying to call a js function with this value and show data accordingly. But I am not getting any data, I assume it's for the URL.

How can I achieve that? Thanks!

post data from a.php (search=test) b.php?search=test

$(document).ready(function() {
    var $_GET = {};
    if(document.location.toString().indexOf('?') !== -1) {
        var query = document.location
                       .toString()
                       .replace(/^.*?\?/, '')
                       .replace(/#.*$/, '')
                       .split('&');
        for(var i=0, l=query.length; i<l; i++) {
           var aux = decodeURIComponent(query[i]).split('=');
           $_GET[aux[0]] = aux[1];
        }
         callA("category",$_GET['search']);
    }    
});

 <?php echo '<form action="../b?search=<?php $search_all ?>" method="get" style="display:inline;">';?> <input type="text" name="search" id="search" value="Search by film, director or keyword" onblur="if (this.value == '') {this.value = 'Search';}" onfocus="if (this.value == 'Search by film, director or keyword') {this.value = '';}" /> <input type="submit" /> </form> 

You can get the data from url on your javascript by just using the php command in it. see sample code below:

 <script> var search = '<?php echo $_GET['search'] ?>'; alert(search); </script> 

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