0

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>

5
  • "And by using the following code I am able to get the value." How do you know, that you got the value? How did you verify that? Also, what does the code you use to send the value in a.php look like? Commented Dec 18, 2017 at 7:12
  • alert($_GET['search']); // to print the value Commented Dec 18, 2017 at 7:14
  • What do you need php for in this example? BTW: In javascript you can get the query string with location.search Commented Dec 18, 2017 at 7:14
  • <?php echo '<form action="../catalogue-test?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> Commented Dec 18, 2017 at 7:15
  • Please put the code in your original post and not into the comments. Commented Dec 18, 2017 at 7:18

1 Answer 1

1

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>

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

1 Comment

thanks! by using my code I am able to get the data but I want to call a js function using that data and show the results.

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.