views:

73

answers:

1

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

A: 

You'll need to output the PHP array in a way the Javascript can interpret, like as a Javascript array:

<?php
echo "<script type=\"text/javascript\">\n";
echo "var links = [";
foreach($links as $link)
    echo "'$link', ";
echo "];\n";
echo "</script>\n";
?>

You also need an element in the form the function can set:

<input type="hidden" name="url">

Then your Javascript function can index into the array and modify the form element:

function pass() {
    id = parseInt(document.getElementsByTagName('form')[0].lcd.value, 10);
    document.getElementsByTagName('form')[0].url.value = links[id];
}

Your button values have spaces around them for some reason; they should just be the number, or lcd.value will 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 $_POST on the other end, so change the tag to:

<form name="keypad" id="form" method="post" onSubmit="return pass()">

You can also just use $_GET on the target page

Michael Mrozek
thanku you Michael.. let me try this...
Kiran George
error heppening when i tried to use the "for($links as $link) .... Is that because my coding problem, i am a beginner in php
Kiran George
can any body suggest a better idea. the **echo** idea is ruining my page with links
Kiran George
@Kiran My mistake, I wrote `for` instead of `foreach`. And you need to put the PHP part in script tags so the output is parsed as Javascript; I added one way to do it in my example
Michael Mrozek
ok Michael, let this try this one....
Kiran George
Kiran George
My Browse.php is accepting variables as ** $url = $_POST['url'];$var = fread_url($url);// function calling to get the page from curl ** I am new to php/javascript and i am forced to use it for a project.. Any help
Kiran George
@Kiran I changed some more things based on your comments
Michael Mrozek
@Michael Mrozek . First of all, thx for ur support. But stills i am not able to pass it eventhough i am getting no errors. I like to show you my code. Do i make a new thread and post my entire code there. I am calling 'browse.php' from within 'browse.php' ... Is that the reason for error.....
Kiran George
I changed the method to **GET** now the url should be passed like this **http://localhost/eye/browse.php?url=http%3A%2F%2Fisrc.ulster.ac.uk
Kiran George
The code worked after making a small change ** echo "<script type=\"text/javascript\">\n";echo "var links = [";echo '"' . $link[0] . '"';for($k=1;$k<$i;$k++){ echo ',"' . $link[$k] . '"'; }echo "];\n";echo "</script>\n";**@ Michael Mrozek , thxxx
Kiran George