I have a javascript code that has some variables which stores information in them. Now I want this html page containing javascript to transfer these variables to a new php page. How can I achieve this?
Please let me know. Thanks a lot in advance for your reply.
appending the variables to a url as a query string in an <a>
use ajax to send the variables to a php page
I dont want to use cookies and I don’t have an idea about ajax. Can you give me further idea about how do I append variables to a url as a query string? Is it by using get and post methods? But I believe get and post methods are not valid in javascript?
it looks like you want to just the send the values entered for first and last name to a php script.
If that is the case, you don’t need the javascript at all.
You just need to add some parameters to your <form>, change the button to type=“submit” and then submit the form.
In the code below, your php script will receive to variables called
$_POST[‘value1’] //will contain the first name
$_POST[‘value2’] //will contain the last name
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form action="myPhpScript.php" method="post">
First name: <input type="text" name = "value1" value = ""/><br />
Last name: <input type="text" name = "value2" value = ""/>
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
When the user enters his first name and last name and on clicking submit, it should be directed towards a javascript function where some encryption needs to be done and after that the encrypted values are to be stored in point 1 and point 2. I have not included the encryption thing because I thought I would figure it out later.
I haven’t done any error checking and I assume this is just an exercise and not a real lfe application because as pmw57 points out, this is not a secure way of doing things.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function sendForm() {
var formO = document.getElementById("frm1");
//reset the textbox values to the encrypted values
document.getElementById("value1").value = encryptStr(document.getElementById("value1").value);
document.getElementById("value2").value = encryptStr(document.getElementById("value2").value);
//now submit the form
formO.submit();
}
function encryptStr(str) {
//put your encryption code here
return strOut;
}
</script>
</head>
<body>
<form id="frm1" action="myPhpScript.php" method="post">
First name: <input type="text" name = "value1" id="value1" value = ""/><br />
Last name: <input type="text" name = "value2" id="value2" value = ""/>
<input type = "button" value = "Submit" onclick="sendForm();" />
</form>
</body>
</html>
With form method = “post” I think they are added to the headers of the http request to the url of the action attribute in <form>
With form method = “get” they are appended to to the action url as a query string (name value pairs of all your form inputs) and so everyone can easily see what is being sent.
The php script receives the sent form data in arrays called $_POST or $_GET depending on the form’s method value.
So in the code I posted, your php script will receive 2 variables called