1

I am trying to pass two values from a page to another page using javaScript as follows:

window.location.href = "changefile.php?equation="+s+"&constant="+c; 

Inside changefile.php I am getting the values as

$eqn =$_GET['equation'];
$cons=$_GET['constant'];

It is working fine for all the cases except:

Suppose the value of equation is x+1.

I am passing it using window.location.href = "changefile.php?equation="+s+"&constant="+c;

I am getting as x 1 as output. The + is missing.

Please help me. What I am doing wrong?

2 Answers 2

2

You should use encodeURIComponent() to encode s and c parameters.

The encodeURIComponent() method encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).

window.location.href = "changefile.php?equation="+encodeURIComponent(s)+"&constant="+encodeURIComponent(c); 
Sign up to request clarification or add additional context in comments.

Comments

1

You need to encode the parameter value using encodeURIComponent

window.location.href = "changefile.php?equation=" + encodeURIComponent(s) + "&constant=" + encodeURIComponent(c);

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.