0

How to send multiple data as URL parameter to server side php script using only Javascript/Ajax.

I don't need to use Jquer.y

I am tying this way:

xhttp.open("GET", 'spec_crawler.php?value='+postValue+'&tablename='+tablename+'&id='+postProdID+'\'', true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send();

On server side I am getting only value:

$html_snippet =$_GET['value'];

Others are empty. but from client side I am sending the proper value.

Am I missing something fundamental?

2
  • 1
    That should work fine? You probably want to URL encode the variables though. Commented Mar 13, 2016 at 19:03
  • 1
    Uhm, maybe not, what's the +'\'' at the end for, looks invalid, you can't end the URL like that after the querystring ` Commented Mar 13, 2016 at 19:05

1 Answer 1

1

Content type "application/x-www-form-urlencoded" is usually used for POST requests.
Encode each param value with encodeURIComponent function:

var params = 'value=' + encodeURIComponent(postValue) +'&tablename=' + encodeURIComponent(tablename) +'&id='+ encodeURIComponent(postProdID);
xhttp.open("GET", 'spec_crawler.php?' + params, true);          
xhttp.send();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

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

7 Comments

Do i have to decode value on the server side? Actually the value is a large html source code. Please suggest
you don't need to decode URL-encoded string on server side. From PHP documentation: Warning: The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results.
problem i am facing is encodeURIComponent(postValue) is not giving me any response from server if the postValue is the large html source code i need to send. But when i am sending small string in postValue that works. I guess there is a problem with encodeURIComponent. Do you have any idea?
another thing it's working fine on my local pc. But not working on my linux server (my godaddy server)
you shouldn't pass a large html source code via GET params. Use POST request for those cases
|

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.