Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP access to form variables

Posted on 2011-05-05
8
Medium Priority
?
331 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:steva
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Accepted Solution

by:
cfEngineers earned 2000 total points
ID: 35700565

<?php
$numberOfBooks = $_POST["numberOfBooks"];
?>

and if you want to grab the variable from the url instead

$numberOfBooks = $_GET["numberOfBooks"];

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35700747
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.
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 35700778
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35700787
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

0
 

Author Comment

by:steva
ID: 35700806
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.
0
 

Author Comment

by:steva
ID: 35700825
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...
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 35700844
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.
0
 

Author Comment

by:steva
ID: 35700854
Yes, so we all agree.  Javascript is the answer.  

Ray, thanks for code post.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question