Link to home
Start Free TrialLog in
Avatar of steva
steva

asked on

PHP access to form variables

Is there any way that PHP can access form variables?  Suppose I have a form, for example, that has:

Please enter the number of books you want to buy [  6     ]
Please enter the names of the 6 books you want to buy

I know this is a silly example but it illustrates what I basically want to do:  I want to use a number that the user entered into a previous text input of the form to phrase a later question in  the form.

I've been so impressed with PHP so far that it's hard to believe that it can't do this.

Thanks for any thoughts.
ASKER CERTIFIED SOLUTION
Avatar of cfEngineers
cfEngineers

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The answer to your question is, "Yes, PHP can access form variables."  But maybe not exactly the way you are hoping.  Here is the basis for my answer:
http://us.php.net/manual/en/tutorial.forms.php

For PHP to receive the "6" you would need to submit the form.  Then PHP can process the number "6" and create another form with the 6 input controls for the title.

If you add some JavaScript into the mix (jQuery comes to mind) you can control the form all on the same client page without submitting an HTTP request.
as cfEngineers says it how you access the form values depends how you set up your form, if it was a post method then you access it with $_POST["name of form field"] or for the get method it goes $_GET["name of form field"]
What you get from the form page is an associative array of values with the keys being the name of any field that was in the form.
Here is an old example I used to show how you can extend the contents of a form by using JavaScript.  The action-script part of this just dumps the POST information so you can see where the data from the form went when it was given to your PHP script.

HTH, ~Ray
<?php // RAY_morefields.php
error_reporting(E_ALL);

// ANYTHING POSTED YET?
if (!empty($_POST))
{
    echo "<pre>";
    var_dump($_POST);
    die();
}

// NOTE: THE $_POST ARRAY CONTAINS AN ARRAY FOR EACH OF THE FORM INPUT VALUES
// THE ZERO POSITION IN THESE ARRAYS IS EMPTY, AN ARTIFACT OF THE INITIAL OCCURRENCE OF DIV "readroot"
// THE FORM FIELDS INSIDE THIS DIV ARE SUBMITTED, BUT WILL NOT BE FILLED IN BECAUSE THE DIV IS INVISIBLE TO THE CLIENT

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript - Extending forms</title>
<!-- FROM QUIRKSMODE -->
<script type="text/javascript">
<!--

function moreFields() {
	var newFields = document.getElementById('readroot').cloneNode(true);
	newFields.id = '';
	newFields.style.display = 'block';
	var insertHere = document.getElementById('writeroot');
	insertHere.parentNode.insertBefore(newFields,insertHere);
}

window.onload = moreFields;

// -->
</script>
</head>

<body>

<h3>Example</h3>

<form id="request" action="" method="post">
<div id="readroot" style="display:none">
<input type="button" value="Remove transcript destination" onclick="this.parentNode.parentNode.removeChild(this.parentNode);" src="delete.png" alt="Remove transcript destination"/>
<br />

<label for="HowMany">Number of transcripts to be sent to this address</label>:
<br />
<input name="HowMany[]" id="HowMany" class="required" title="Number of transcripts" maxlength="2" size="2"/>
<br />

<label for="Name">Name</label>
<br />
<input name="Name[]" id="Name" class="required" title="Enter the name" maxlength="25"/>
<br />

<label for="DCity">City</label>
<br />
<input name="DCity[]" id="DCity" class="required" title="Enter your City" maxlength=""/>
<br />

<label for="Timing">When Should the transcript be sent?</label>
<br />

<select id="Timing" name="Timing[]"  class="validate-selection">
<option value="X">Select</option>
<option value="1">Now</option>
<option value="2">When semester grades are posted</option>
<option value="3">When degree statement is posted</option>
</select>
</div id="readroot">

<span id="writeroot"></span id="writeroot">

<div class="buttons">
<button type="button" id="btnMore" onClick="moreFields();" value="Add another transcript destination" class="positive">
<img src="add.png" />
Add another transcript destination
</button>

<button type="submit" class="positive">
<img src="bullet_go.png" />
Submit Completed Form
</button>
</div class="buttons">
</form>

</body>
</html>

Open in new window

Avatar of steva
steva

ASKER

Ah, but you're doing this from the action item program that process the form.  I want to do it from the page that has the form, before the form has been submitted.

But as I'm writing this I'm beginning to realize that the question really doesn't make any sense.  PHP only runs in the server, and the form is being filled out in the client. If there were any way to do this it would be with Javascript, not PHP.

I gave you the points anyway, since your answer is, I believe, the closest.

Thanks.
Avatar of steva

ASKER

Whoa!

While I was answering cfEngineers post a slew of new posts came in that I hadn't seen. Let me take a look and I'll get back...
if you want to access a form field with out posting a page you can do it like this.
document.form1.fieldname.value which is using JavaScript and it will give you the current value of a form field with out submitting it.
Avatar of steva

ASKER

Yes, so we all agree.  Javascript is the answer.  

Ray, thanks for code post.