php equivilant of javascript's formname.name1.value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
twooton
Forum Newbie
Posts: 3
Joined: Mon Dec 19, 2005 12:41 pm

php equivilant of javascript's formname.name1.value

Post by twooton »

I did a search on this and found that

Code: Select all

$_POST['name1']
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 :D
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

It depends on your form method post or get

If you use Post

Code: Select all

$_POST['name1'];
If you use Get:

Code: Select all

$_GET['name1'];
twooton
Forum Newbie
Posts: 3
Joined: Mon Dec 19, 2005 12:41 pm

Post 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

Code: Select all

$_POST['name1']
for just the form named 'form1'
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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>';
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you don't have access to the form names during a submission
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
twooton
Forum Newbie
Posts: 3
Joined: Mon Dec 19, 2005 12:41 pm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Read feyds last reply.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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>
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
Post Reply