views:

123

answers:

1

Hi,

I'm trying to pass a url like:

my_url = 'http://somedomain.com/somepath/somepage?key="query"';

when someone clicks on a form.

I tried using encodeURI and encodeURIComponent and even using alerts to see that I have either "query" or %22query%22 because I call something like document.my_form.action = my_url but when the browser hits to that page, I get:

http://somedomain.com/somepath/somepage?key=query

The double quotes are missing. Similar tries with '=', '@' all are retained... is there something I'm missing?

Thanks.

+1  A: 

Using a query string in an action attribute of a GET-method form will result in the query being overwritten by the variables of the form itself.

You can try to add your values as hidden input fields (using JavaScript).

I tried your code, and it works great for me:

<?php
 print_r($_GET);
?>
<br>
<form id="testForm" method="post">
 <input type="submit">
</form>

<script>

 var actionUrl = "http://domain.com/test.php?key=" + '"query"';

 document.getElementById("testForm").action = actionUrl;

</script>

Output:
Array ( [key] => "query" )
Matthias Vance
It's allowed, but yeah, in the case of a GET-method form the browser will overwrite the query string with any input values in the form. The query string is untouched in the case of a POST form, but not all server-side environments will bother read both the query string *and* the post-body in that case.
bobince
Right, I was testing on a GET-method form.
Matthias Vance
Thanks for the suggestions... indeed when I used 'post' the double quotes are not removed but I'm still stuck somehow at getting an invalid JSON object response... more debugging!
Pydroid