Page 1 of 1

Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 4:12 pm
by lord_zelo
I am Very new to PHP but not too new to programming in general. I just have some questions about PHP and submitting a form to it's self.

I have a simple form with validation that gets submitted to itself. That all works fine and dandy, but what my problem is is when the form gets submitted to itself I lose my variables if the form doesn't validate. I've tried many different ways to put my vars into the form and have then show up when the form doesn't validate, but I just can't seem to get it to work.

Here is my code:

Code: Select all

 <?php
 
//the form
$form='
    <form method="post">
 
    <input type="hidden" name="s" value="y"/>
 
    Name: <input type="text" name="name" value="IWANTMYVALUEHERE"/><br/>                                 
    Age: <input type="text" name="age" value="IWANTMYVALUEHERE"/><br/>  
    <input type="submit"/>
 
    </form>
    ';
 
//check the form
 
//if form has been submitted
if ($_POST['s']=='y')
    {
    //if name and age
    if (($_POST['name']!=null) && ($_POST['age']!=null))
        {
        //output name and age
        echo $_POST['name'] . ' is ' . $_POST['age'] . "\n";
        }
    //if not name and age
    else
        {
        //output form
        echo $form;
        }
    }
//if form has not been submitted
else
    {
    //output form
    echo $form;  
    }
?>
It seems that I can't just put a variable in like &_POST['name'] etc.. for the value of my input and have it show when the $form var is called. I get a syntax error about an unexpected T_STRING when I do that. You can see where I would like my value's to show in the form if you look at the code. I want to make this as Simple as possible.

Can anyone give me some advice or help with this? It would be much appreciated.

LZ

Re: Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 4:33 pm
by JAB Creations
Is this what you want to do?

Code: Select all

<?php
$myval1 = 'one';
$myval2 = 'two';
 
//the form
$form="
    <form method=\"post\">
 
    <input type=\"hidden\" name=\"s\" value=\"y\"/>
 
    Name: <input type=\"text\" name=\"name\" value=\"$myval1\"/><br/>                                
    Age: <input type=\"text\" name=\"age\" value=\"$myval2\"/><br/>  
    <input type=\"submit\"/>
 
    </form>
    ";
 
//check the form
 
//if form has been submitted
if ($_POST['s']=='y')
    {
    //if name and age
    if (($_POST['name']!=null) && ($_POST['age']!=null))
        {
        //output name and age
        echo $_POST['name'] . ' is ' . $_POST['age'] . "\n";
        }
    //if not name and age
    else
        {
        //output form
        echo $form;
        }
    }
//if form has not been submitted
else
    {
    //output form
    echo $form;  
    }
?>

Re: Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 4:39 pm
by nowaydown1
I may have missed the mark on what you're trying to do with this, so if that's the case I apologize in advance. Here's a simple example of how to have your form post onto itself, and keep the values persisted if the validation should fail. In the "real world" you would want to make sure you sanitize the user input.

Code: Select all

 
<?php
 
if(isset($_POST["btnSubmit"])) {
        
    if(!empty($_POST["name"]) && !empty($_POST["age"])) {
        printf("Your name is %s and your age is %s.", $_POST["name"], $_POST["age"]);
        exit;
    } else {
        echo "Please enter both a name and age.";
    }
}
 
?>
 
<form method="post" action="<?php print($_SERVER["PHP_SELF"]); ?>">
    <label for="name">Name:</label>
    <input type="text" name="name" value="<?php print($_POST["name"]); ?>" /><br />
    <label for="age">Age:</label>
    <input type="text" name="age" value="<?php print($_POST["age"]); ?>" /><br />
    <input type="submit" name="btnSubmit" value="Submit" />
</form>
 

Re: Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 4:43 pm
by lord_zelo
JAB Creations:
That is exactly what I want.
Do I have to put my $form = "bleh" in double quotes and escape all the double quotes in the html? Isn't there an easier way to do that?

nowaydown1:
That is close, but I don't want the form to be displayed when the form validates, I only want the final result to be displayed. I like the way you checked to see if the form was submitted though. :D

Re: Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 4:51 pm
by JAB Creations
In PHP single and double quotes work differently. Double quotes will allow you to push data dynamically, single quotes will spit out exactly what you see.

Code: Select all

$myval1 = 'one';
$myval2 = 'two';
 
echo '$myval1'; // echos '$myval1'
echo "$myval2"; // echos 'two'
To avoid all the excess quote escaping use concatenation which are periods that connect things.

Code: Select all

$one = '111';
$two = '222';
echo $one.$two; // echos '111222'
Fragment your form as so...

Code: Select all

$form1 = '<form>';//first part of your form here
$form2 = '</form>';
$myvar = 'hi';
echo $form1.$myvar.$form2;
You'll avoid having to escape everything then.

Re: Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 4:56 pm
by lord_zelo
Oh man, concatenating the form is Almost as bad as escaping all the double quotes.

There has to be another way... Must find another way...

How did you get your code to be color coded by the way?

Re: Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 5:21 pm
by JAB Creations
BBcode which I have disabled in this post so you can see exactly what to use...

[syntax=php][/syntax]

[javascript][/javascript]

[css][/css]

[html][/html]

To be frank I'm not wild about mixing PHP and (X)HTML code; if I have to I keep it to an absolute minimal. Makes it much easier to work through newer versions without having to make major modifications.

Re: Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 5:30 pm
by lord_zelo
JAB,
Good idea. I still need to develop some good PHP coding habits. I'll take what you said into consideration. Thanks for the help with color coding things.

Re: Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 5:31 pm
by aceconcepts
What you want to do is call your validation at the top of the page and then output your form with the variables echoed in the element's value.

Re: Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 5:36 pm
by JAB Creations
:yar:

...and no problem. :mrgreen: I'm just glad I could be the helper this time. :D

Re: Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 5:44 pm
by lord_zelo
aceconcepts,
That seems to be what JAB was getting at in his first reply to my post.

My prediciment(could be called my Laziness) is that I hate to make multiple files for forms if I can help it. In a custom programming language that I currently use that was written by a close friend of mine, it makes things like this so much easier, it's almost a drudge writing the PHP to do it.

In the language that I am used to writing, I would have writting this same form validation I have been adking help about as follows:

Code: Select all

{set form}
  <form method="post">
  <input type="hidden" name="s" value="y">
  <input type="text" name="name" value="[name]"/>
  <input type="text" name="name" value="[age]"/>
  <input type="submit"/>
  </form>
{/set}
 
//has form been submitted
{if [s]}
   //if name and age
  {if [name] and [age]}
    Hello [name] you are [age] years old.<br/>
  {else}
    [form]
  {fi}
 {else}
  [form]
{fi}
From this, you can kind of see why I want to do the validation the way that I am, this is just the way that I think because of the coding background that I have had with this language(called AMP).

Re: Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 6:25 pm
by aceconcepts
In that case, instead of declaring individual variables you would simply work from the $_POST[] array:

Code: Select all

 <input name="some_name" type="text" value="<? echo $_POST['some_name']; ?>" />

Re: Submit form to self, have vars show as input values

Posted: Sun Apr 27, 2008 6:31 pm
by RobertGonzalez
This is just one way of thinking about this....

Code: Select all

<?php
// Default values
$action = basename(__FILE__);
$name = isset($_POST['name']) ? $_POST['name'] : null;
$age = isset($_POST['age'])? $_POST['age'] : null;
 
// Validation flag
$valid = false;
 
// Default output
$echo = '';
 
//check the form
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    //if name and age
    if ( !empty($name) && !empty($age) ) {
        //output name and age
        $valid = true;
        $echo  = $name . ' is ' . $age . "\n";
    }
}
 
if ($valid) {
    echo $echo;
} else {
//the form
$form= <<<THEFORM
<form method="post" action="$action">
    <input type="hidden" name="s" value="y"/>
    Name: <input type="text" name="name" value="$name"/><br/>
    Age: <input type="text" name="age" value="$age"/><br/>
    <input type="submit"/>
</form>
THEFORM;
 
echo $form;
?>