I have a PHP array:
foreach($xpath->query('//a') as $element) {
$linklabel[] = $element->textContent;
$link[] = $element->getAttribute("href");
$i=$i+1;
}
I also have an HTML form:
<form name="keypad" id="form" onSubmit="return pass(this)">
<input type="text" size="100%" length="25" value="" name="lcd">
<input type="button" value=" 1 " name="one" onClick="this.form.lcd.value+=this.value">
<input type="button" value=" 2 " name="two" onClick="this.form.lcd.value+=this.value">
<input type="button" value=" 0 " name="zero" onClick="this.form.lcd.value+=this.value">
<input type="submit" name="Submit" id="Submit" value="Go" >
</form>
How can I write the onSubmit function pass() that takes the values from the keypad and sends the corresponding $link[] value to browse.php. For example, if $link[20]='google.com'; , I want to pass $link[20] to browse.php if user enters 20 in the keypad and presses Go.
You’ll need to output the PHP array in a way the Javascript can interpret, like as a Javascript array:
You also need an element in the form the function can set:
Then your Javascript function can index into the array and modify the form element:
Your button values have spaces around them for some reason; they should just be the number, or
lcd.valuewill end up with spaces throughout it. If you want the buttons to have padding use CSS.Finally, you need to post the form if you’re going to check
$_POSTon the other end, so change the tag to:You can also just use
$_GETon the target page