1

I have following php variable of name

    $name=$_POST["name"];
    <script>
    pge = '<?php echo $name ;?>';        
    var url = "searchCustomerByNameApi2.php"
    //some code 
   xmlhttp.open("GET", url, true);
   xmlhttp.send();

how can I send pge variable along with url as aquery string and access it on the php page ??

3
  • 1
    Try "searchCustomerByNameApi2.php?pge="+pge and get it on php page like $_REQUEST['pge']. Commented Jul 13, 2017 at 6:06
  • thanks a lot it works Commented Jul 13, 2017 at 6:13
  • Added this as answer, please accept and upvote, if it was helpful Commented Jul 13, 2017 at 6:14

4 Answers 4

1

Simple you have your javascript code look like this.

<script>

   var  name  = '';

   name = '<?php print $_POST["name"]?>';   
   var url = "searchCustomerByNameApi2.php?name"+name;
   xhttp.open("GET", url, true);
   xhttp.send(); 

</script>

and php code look like searchCustomerByNameApi2.php page

   $name = $_REQUEST['name'];
Sign up to request clarification or add additional context in comments.

Comments

1

Try "searchCustomerByNameApi2.php?pge="+pge and get it on php page like $_REQUEST['pge'].

Comments

0

You can use a function to create a query string from an object:

And append that result to your url:

$name=$_POST["name"];

<script>
function encodeQueryData(data) {
   let ret = [];
   for (let d in data)
          ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(data[d]));
   return ret.join('&');
 }

pge = '<?php echo $name ;?>'; 
var data = {pge: pge};
var query =encodeQueryData(data);       
var url = "searchCustomerByNameApi2.php";
url += "?" +query;
xmlhttp.open("GET", url, true);
xmlhttp.send();

Comments

0

Have you tried below?

var url = "searchCustomerByNameApi2.php";
var test = "name=<?php echo $_POST["name"]; ?>";

xmlhttp.open("GET", url, true);
xmlhttp.send(test);

Hope this resolve your issue.

Comments

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.