Log in

View Full Version : Passing on the current value of a field



benmajor
04-19-2007, 09:44 PM
Sorry if this question has already been asked before, but I really am a n00b when it comes to coding with JavaScript.

What I am looking to do is pass on the current value of a field into a pop-up window. The value needs to be written into a PHP variable. How I do this does not really matter. The method I have used thus far is as follows:



<input class="mainField" name="to" type="text" id="to" size="50" />

<script type="text/javascript" language="JavaScript">
var value = document.compose.to.value
</script>

<input name="button" type="button" class="btn" onclick="window.open('userSearch.php?field=to&value=+value','_search','width=400,height=200,left=100,top=100,resizable')" value="Find Users" />


What I am basically looking for is a really short script that takes the current value from the field named "to" and passes it to the opened window. I wondered whether it was possible to write a JavaScript variable to a PHP variable, for example:



$value = ?><script language="JavaScript">var value = "document.compose.to.value";
document.write(value);
</script>
<?php ;?>


Any help with this problem really would be gratefully received.

Many thanks in advance,
Ben

thetestingsite
04-20-2007, 04:25 AM
Beings that you are going to be assigning the PHP variable anyways, why not just assign it with the value from the form directly. In other words:



<?php
$value = $_REQUEST['to']; //get the form field with the name "to".

//rest of code here
?>


Unless I am mising something, or misunderstood you post, it appears that this is pretty much all that you wanted. Although, if you wanted it to open in a new window, you could just do something like this:



<form action="userSearch.php" method="POST" target="_blank">


That will open userSearch.php and it's results in a new browser window.
Hope this helps.

benmajor
04-20-2007, 07:03 AM
thetestingsite,

Many thanks for your response. The reason that I did not use the $_REQUEST variable in PHP is due to the fact that I don't actually want to submit the form.

Basically, I want to have a field that contains a list of usernames, for example user1,user2. Each time the "Find User" button is pressed, it should open the userSearch.php window, and pass on the current value of the field, so that another username can be appended to the end of the string (hope that makes sense).

So basically, I would use opener.document to write the username to the field, but because I need to append each username, I need to maintain the current value, for example:


<select name="course" class="mainField" onchange="javascript:window.opener.document.compose.<?php echo $field;?>.value = <?php echo $currentValue;?>,(this.value)">

I hope that all makes sense, it is pretty difficult to put all of this into words

Many thanks once again,
Ben

Twey
04-20-2007, 09:55 AM
No, you can't.

PHP is executed server-side; this means it finishes executing before the Javascript is even sent to the browser. The only way it can do anything is by executing again, which involves requesting the page again.

mburt
04-20-2007, 10:33 AM
The only way it can do anything is by executing again, which involves requesting the page again.
Which would remove the point of client-side technologies.