2

I have a page here prompt get an input and stores in a variable called ans,i need to pass this value to y.php

<script type="text/javascript">
function s()
{
var ans = prompt("Name your query?","");
}
</script>
<input type="image" src="images/savn.jpg" onclick="s();" />
<form method=post" action="y.php">

below here it has some text boxes so,i need this values also to be posted to y.php

I am struck.Can,any one take me further to next step?

2 Answers 2

2

Place on your page:

<input type="hidden" id="ans" name="ans" value="" />

Following the line var ans = prompt("Name your query?","");, add the line:

document.getElementById("ans").value = ans;

This should guarantee that you can access the variable "ans" on the page y.php as variable $_POST['ans'].

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

7 Comments

I am getting this [object HTMLInputElement] in y.php.I did not get prompt input.
I'm assuming the function s gets run first, so presumably you'd click on the image first and then submit? You should see some sort of prompt.
@Neil:I need to save only after two text boxes.How do i do it?
Add a function testValid which checks if values have been inserted: if(document.getElementById('textbox1').value.length == 0) return false; and add that method in the event onsubmit="return testValid()" in the form tag. You can move the prompt for ans after you do the validation so it only asks when you've inserted valid values. Hope that helps.
@neil:i have a textb enter data and text box2 enter data so here above prompt should get ip only if 2 text box values are present
|
1

Create a hidden form element and set it.

<script type="text/javascript">
function s()
{
  document.getElementById('ans').value = prompt("Name your query?","");
}
</script>
<input type="image" src="images/savn.jpg" onclick="s();" />
<input type="hidden" name="ans" id="ans" />
<form method=post" action="y.php">

Now when you submit the form, the form variable will be submitted back.

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.