Forum Moderators: open

Message Too Old, No Replies

oninput numbers change value of looping

         

nanat

10:21 am on Dec 2, 2009 (gmt 0)

10+ Year Member



function validate (form) {
var TestVar = form.inputbox.value;
alert ("You typed: " + TestVar);
}

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>

rocknbil

6:38 pm on Dec 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have no form element named inputbox.

nanat

12:39 am on Dec 3, 2009 (gmt 0)

10+ Year Member



tnx rocknbil i get ^^..

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>

nanat

1:14 am on Dec 3, 2009 (gmt 0)

10+ Year Member



aw.. my problem is it will refresh the whole page and it will reset the value inside my $_POST statement

my question is:
how onchange with out refreshing my page in order for me to retain my value inside my $_POST function..

rocknbil

1:16 am on Dec 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



EDIT: Ah, we're posting at the same time. I get you now, at least I THINK I do. :-)

<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." }

nanat

1:48 am on Dec 3, 2009 (gmt 0)

10+ Year Member



exactly rocknbil. my orignal form:

<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? :(

rocknbil

7:28 pm on Dec 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See important note at "simpler solution" below. I think you're doing this the hard way.

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 + '&amp;' +
form.DoCNO.value + '&amp;' +
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>

nanat

1:10 am on Dec 4, 2009 (gmt 0)

10+ Year Member



asome rocknbil i got it ^^:


var locString = form.inputbox.value + '&DoCNO=' +
form.DoCNO.value + '&NumAt=' +

honestly i don't like the structure of my code because so complicated and hard coded.. weeew.. nanat level up to 20..

rocknbil

2:37 am on Dec 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, take a minute and consider the last part of that post. You're doing it the hard way. :-)