Page 1 of 1
php equivilant of javascript's formname.name1.value
Posted: Mon Dec 19, 2005 12:47 pm
by twooton
I did a search on this and found that
will return the string value stored in the textbox 'name1'. Is this the only way of getting the javascript equivilant of formname.name1.value? Because i cannot get it to return a value. Thanks in advance

Posted: Mon Dec 19, 2005 12:51 pm
by hawleyjr
It depends on your form method post or get
If you use Post
If you use Get:
Posted: Mon Dec 19, 2005 12:56 pm
by twooton
I'm using the POST method. I'm reverse engineering someone else's code and I noticed he has two forms that have the name 'name1' ... I imagine this presents a problem. How do I use
for just the form named 'form1'
Posted: Mon Dec 19, 2005 12:57 pm
by hawleyjr
It only matters which form is being submitted. Try this code to see what is being submitted:
Code: Select all
echo '<HR><PRE>'; print_r($_POST); echo '</PRE>';
Posted: Mon Dec 19, 2005 12:58 pm
by feyd
you don't have access to the form names during a submission
Posted: Mon Dec 19, 2005 1:29 pm
by John Cartwright
Usually this is delt with by some unique field that can identify which field has been submitted. For instance, the value of the submit button is different for each page -- although this fails if the user presses enter instead of clicks the button. So a better way to handle it would be to use a hidden field to identify the forms.
Posted: Mon Dec 19, 2005 1:31 pm
by twooton
is there anyway to get the value at form1.name1.value without having to use the $_POST['name1']. such as just getting an alert box to pop up and say its name:
Code: Select all
alert("<?php echo $_POST['name1']; ?>")
The code above obviously never works, but somethign to that extent.
Posted: Mon Dec 19, 2005 1:33 pm
by John Cartwright
Read feyds last reply.
Posted: Mon Dec 19, 2005 1:41 pm
by hawleyjr
Not the best way to do it but here you go:
Code: Select all
<script language="JavaScript">
<?php
if(isset($_POST['field_1'])){
echo 'alert("' . $_POST['field_1'] . '");';
}
?>
</script>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="form1">
<input name="field_1" type="text" value="some value">
<input name="Submit" type="button" value="Submit">
</form>
Posted: Mon Dec 19, 2005 2:08 pm
by patrikG
twooton wrote:is there anyway to get the value at form1.name1.value without having to use the $_POST['name1']. such as just getting an alert box to pop up and say its name:
Code: Select all
alert("<?php echo $_POST['name1']; ?>")
The code above obviously never works, but somethign to that extent.
Javascript has access to it's specific host-browser's DOM (i.e. Document Object Model) which is slightly different from browser to browser. That is why you can access e.g. "form1.name1.value" at runtime and get a value back immediatly. Because Javascript can access the browser's DOM and in fact is part of the browser, it's a client-side language.
PHP can only understand input that is handed to it via the HTT-protocol (I'm ignoring db & filesystem here). The host of the HTT-protocol is the server. Hence, PHP is a server-side language.
The client can communicate with the server only via the HTT-protocol (again for the sake of the argument I'm ignoring FTP, gopher etc.). Hence: Form is submitted -> browser sends form-information via HTT-protocol to server -> server sends it to required script that handles everything else. In answer to your question: no.
However, you may find AJAX interesting, which is much more dynamic and is - sort-of- a pushy little express train on the HTT-protocol in constant contact with the server. With that, you can achieve your example using writing to a <div> or other writable HTML-elements.