Forum Moderators: open
how can i pass a value of TestVar and submited to $value in php?
so if ever i input a number it will automatic query..
<table width="1060" border="0"><?php
$value=3;
for($i=0; $i<$value; $i++)
{
?>
<tr>
<td width="333"><input type="text" name="Project[]" size="55"/></td>
<td width="273"><input type="text" name="remarks[]" size="45"/></td><?
echo '<input type="hidden" name="i[]" value='.$i.' />';
}
echo '<input type="hidden" name="edit" value="edit"/>';?>
<td width="432"></td>
</tr></table>
function validate (form) {
var TestVar = form.inputbox.value;
location.href="PrintMe.php?Result=" + TestVar;
}
<table width="1060" border="0">
<?php
$value = $_GET['Result'];
for($i=0; $i<$value; $i++)
{
?>
<tr>
<td width="333"><input type="text" name="Project[]" size="55"/></td>
<td width="273"><input type="text" name="remarks[]" size="45"/></td><?
echo '<input type="hidden" name="i[]" value='.$i.' />';
}
echo '<input type="hidden" name="edit" value="edit"/>';?>
<td width="432"></td>
</tr></table>
<script type="text/javascript">
function validate (form) {
var TestVar = form.inputbox.value;
window.location="PrintMe.php?Result=" + TestVar;
return false;
}
</script>
<form method="post" onClick="return validate(this);">
<input type="text" name="inputbox">
</form>
Note the return false and the event in onSubmit. Return false stops the form from submitting if Javascript is enabled. Putting it on the form itself instead of a button captures the event when "enter" is pressed - hence, no submit button in my example.
window.location and location.href are just a matter of preference . . .
-------- before edit ---------
Oh, I see, I think. :-) So you are saying inputbox comes from "some other form . . . ."
location.href="PrintMe.php?Result=" + TestVar;
....
$value = $_GET['Result'];
I'm guessing you solved it? You might want to do this:
$value = $_GET['Result'];
if ($value > 0) {
// Do what you already have there in your loop
}
else { echo "OOPS, the value you entered is not a number." }
<FORM NAME="myform" onClick="return validate(this);" method="POST">
<tr>
<td width="130" height="25"><font color='Red'> Desire No.</font><INPUT TYPE="text" size="5" NAME="inputbox" maxlength="2"></td><INPUT TYPE="button" NAME="button" Value="Click" onClick=" validate(this.form)">
</tr>
</FORM>
My problem is it will reset all the values store in
$DoCNO=$_POST["DoCNO"];
$NumAt=$_POST["NumAt"];
how can i retain my value the same procedure like i do? :(
First, change this to onSubmit. Not
<FORM NAME="myform" onClick="return validate(this);" method="POST">
but
<FORM NAME="myform" onSubmit="return validate(this);" method="POST">
Try pressing enter when you type in the inputbox field, you will see why this is needed. Don't forget to return false from validate . . . also see bolded below, you want button, not submit.
My problem is it will reset all the values store in
$DoCNO=$_POST["DoCNO"];
$NumAt=$_POST["NumAt"];
I can't see where these are coming from, or going to. At a guess, if they are available in the myForm page, store them as hidden fields in the form, then pass them in your query string:
<script type="text/javascript">
function validate (form) {
var locString = form.inputbox.value + '&' +
form.DoCNO.value + '&' +
form.NumAt.value;
window.location="PrintMe.php?Result=" + locString;
return false;
}
</script>
<?php
echo '
<FORM NAME="myform" onsubmit="return validate(this);" method="POST">
<table><!-- presuming this is from elsewhere -->
<tr>
<td width="130" height="25">
<!-- font is dead, YAY! font is dead -->
<label for="inputbox" style="color:Red;"> Desire No.</label>
<INPUT TYPE="text" size="5" NAME="inputbox" id="inputbox" maxlength="2">
</td>
<!-- three things to note below:
- use submit, not button, so it still works without JS
- Don\'t use possible reserved names/ID\'s like button, submit, etc
- You already have the validate handler onSubmit, don\'t need it on the button
-->
<INPUT TYPE="submit" NAME="SubmitButton" Value="Click">
<input type="hidden" name="DoCNO" value="' . $_POST['DoCNO'] . '">
<input type="hidden" name="NumAt" value="' . $_POST['NumAt'] . '">
</tr>
</FORM>
</table>
'; // end echo
?>
Then, in PrintMe.php,
$value = $_GET['Result'];
$DoCNO = $_GET['DoCNO'];
$NumAt = $_GET['NumAt'];
if ($value > 0) {
// Do what you already have there in your for loop, then add
echo '<input type="hidden" name="DoCNO" value="' . $DoCNO . '">
<input type="hidden" name="NumAt" value="' . $NumAt . '">
'; // end echo
}
else { echo "OOPS, the value you entered is not a number." }
?>
A much simpler solution: if the above is correct, Javascript is not needed. You're not validating anyway. Instead of setting window.location in Javascript, just make the action of the form PrintMe.php,
<FORM action="PrintMe.php" NAME="myform" onsubmit="return validate(this);" method="POST">
. . . and in PrintMe.php, change all your variable sets to $_POST instead of $_GET.
$value = $_POST['Result'];
$DoCNO = $_POST['DoCNO'];
$NumAt = $_POST['NumAt'];
Only use JS if it's needed . . . . you can still use validate for what the function says it does - to validate the input of inputbox.
function validate (form) {
if (isNaN(form.inputbox.value)) {
alert("Please enter a number.");
}
else { form.submit(); }
return false;
}
</script>