Need help passing Text Field to PHP Variable

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
hwmetzger
Forum Commoner
Posts: 25
Joined: Fri Jun 12, 2009 1:11 pm

Need help passing Text Field to PHP Variable

Post by hwmetzger »

For the life of my I cannot figgure out why it's not working. I have tried everything to get a PHP variable to equal a text field.
This is what I have, it's a basic user account registration script.

Code: Select all

 
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>VV - Account Registration</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
 
<h1>Account Registration</h1>
 
 
<form method="post">
  <label></label>
  <p>
    <label>First Name
    <input type="text" name="fname" id="fname" />
    </label>
     <label>Last Name
     <input type="text" name="lname" id="lname" />
     </label>
  </p>
  <p>
    <label>User Name
    <input type="text" name="uname" id="uname" />
    </label><label></label>
  </p>
  <p>
    <label>Password
    <input type="password" name="pword" id="pword" />
    </label>
     <label>Confirm Password
     <input type="password" name="confrimpword" id="confrimpword" />
     </label>
  </p>
  <p>
    <label>Email
    <input type="text" name="email" id="email" />
    </label>
    <label>Confirm Email
    <input type="text" name="confirmemail" id="confirmemail" />
    </label>
</p>
  <p>
    <label>Secret Question</label>
    <select name="question" id="question">
      <option value="1">What is your mother's maiden name?</option>
      <option value="2" selected="selected">What is the name of your pet?</option>
      <option value="3">In what city were you born?</option>
      <option value="4">What was your first car?</option>
      <option value="5">What is your favorite color?</option>
      <option value="6">What is your favorite movie?</option>
      <option value="7">What is your favorite hobby?</option>
      <option value="8">What is your dream vacation?</option>
      <option value="9">What is your favorite sport?</option>
      <option value="10">What is your worst pet-peeve?</option>
    </select>
    <label>Answer
    <input type="text" name="answer" id="answer" />
    </label><label></label>
  </p>
  </form>
 
 
 
<?php
$entries = array('firstname','lastname','username','password','cpassword','mail','cmail','squestion','sanswer');
        
    
    $CHALLENGE_FIELD_PARAM_NAME = "verificationCode"; 
    
    
    require_once('includes/challenge.php');
 
    //I have tried everything: $_POST, $_REQUEST and $_GET
    if(isset($_POST[$CHALLENGE_FIELD_PARAM_NAME])) {
        $entries['firstname'] = $_REQUEST['fname'];  
        print_r($entries);
        echo $entries['firstname'];
        
        
        if(isChallengeAccepted($_POST[$CHALLENGE_FIELD_PARAM_NAME]) === FALSE) {
            $resultMessage = "The entered verification code was not correct.";
        } else {
        
        
            $resultMessage = "Verification code accepted!";
        }
        
    } else {
        
        $resultMessage = "";
        
    }
 
?>
 
 
 
 
<?php
 
     
    if(!empty($resultMessage)) {
        echo "<p class=\"resultMessage\">{$resultMessage}</p>\n";
    }
 
    
 
?>
 
<form method="post" action=".">
<fieldset>
<legend>Image Verification</legend>
<p>
<label for="subject">Enter Verification Code:</label>
<input type="text" 
    name="<?php echo($CHALLENGE_FIELD_PARAM_NAME) ?>" 
    id="<?php echo($CHALLENGE_FIELD_PARAM_NAME) ?>" 
    maxlength="<?php echo($CHALLENGE_STRING_LENGTH) ?>" 
    size="<?php echo($CHALLENGE_STRING_LENGTH) ?>" 
    class="inputText" />
<img src="getimage.php" />
</p>
<input type="submit" value="Submit" class="submit" />
</fieldset>  
</form>
 
</body>
</html>
 
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Need help passing Text Field to PHP Variable

Post by jackpf »

:? Your script looks a bit confusing tbh...I'm not really sure what you're trying to do.

Try

Code: Select all

print_r($_POST);
and see if it's what you expect.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Need help passing Text Field to PHP Variable

Post by cpetercarter »

I guess that your problem is that you cannot get a value from $_POST[$CHALLENGE_FIELD_PARAM_NAME]. I think you need to have a close look at includes/challenge.php, since I guess that is the script which you think produces this value. Does the script contain a form, and does one of the elements in the form have a name $CHALLENGE_FIELD_PARAM_NAME or 'verificationCode'? Does the form post to the correct script?
hwmetzger
Forum Commoner
Posts: 25
Joined: Fri Jun 12, 2009 1:11 pm

Re: Need help passing Text Field to PHP Variable

Post by hwmetzger »

When I did the print_r($_POST) I noticed it was only pulling the variables from that specific form.
My script has several forms in it. My question is now, how do I pull a variable from a different form?

Something like in VB.net to get a variable from "MainForm.vb" you would do something like
Dim number As Integer = MainForm.thatNumber

Is there something similar in PHP?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Need help passing Text Field to PHP Variable

Post by jackpf »

You'll only get the post data from the form that is submitted. So no, the browser won't send data from other forms.

Can you not combine them into one form?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Need help passing Text Field to PHP Variable

Post by aceconcepts »

Why not increase the scope of a single form?
Post Reply