Jump to content

[SOLVED] why this code do not work ?


asmith

Recommended Posts

i don't know this code is kinda o stupid or not , but it's not working  !

 

<?php
$form_block = "<form action=\"$_SERVER[php_SELF]\" method=\"post\">
name :<input type=\"text\" name=\"user\" />".$msg1."<br />
last :<input type=\"text\" name=\"last\" />".$msg2."<br />
<input type=\"submit\" value=\"check!\" />
</form>";
?>
<html>
<body>
<?php
if (!isset($_POST['submit'])) 
{echo $form_block;}
else{
if ($_POST['user'] == ""){$msg1 = "please enter a your name";$msg3="no";}
if ($_POST['last'] == ""){$msg2 = "please enter a your last name";$msg3="no";}

if ($msg3 == "no"){echo $form_block;}
else {echo "submitted";}
}
?>
</body>
</html>

 

 

this supposed to write the errors beside each fields . but by pressing submitt all the thing reset ! maybe i'm way too tired i can't unserstand this ! :( HELP !

Link to comment
https://forums.phpfreaks.com/topic/78684-solved-why-this-code-do-not-work/
Share on other sites

Course that code wont work as expected. You are using variables before you have defined them!

 

<?php
$msg3 = 'no';
if (isset($_POST['submit']))
{
    $msg3 = 'yes';
    if ($_POST['user'] == ""){$msg1 = "please enter a your name";$msg3="no";}
    if ($_POST['last'] == ""){$msg2 = "please enter a your last name";$msg3="no";}
}

$form_block = "<form action=\"$_SERVER[php_SELF]\" method=\"post\">
name :<input type=\"text\" name=\"user\" />".$msg1."<br />
last :<input type=\"text\" name=\"last\" />".$msg2."<br />
<input type=\"submit\" value=\"check!\" />
</form>";

?>
<html>
<body>
<?php

if ($msg3 == "no")
    echo $form_block;
else
   echo "submitted";

?>
</body>
</html>

Reorganised your code.

too much angry ! at morning i'm sitting working with php , it is an hour this rock is on my way !!

 

thanks for replying ,

but your code when loaded shows "submitted" . i know why .  i change that line to this :

if ($msg3 == "no" || !isset($_POST['submit']))

 

but all the things go reset again !

??? >:( :'(

try:

<?php
$show_form = true;
if(isset($_POST['submit']))
{
    if(empty($_POST['firstname']))
    {
        $error['msg1'] = 'Please provide your firstname';
    }

    if(empty($_POST['surname']))
    {
        $error['msg2'] = 'Please provide your surname';
    }

    if(isset($error) && is_array($error))
    {
        extract($error);
    }
    else
    {
        $show_form = false;
    }
}

?>
<html>
<body>
<?php if($show_form): ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Firstname: <input type="text" name="firstname" value="<?php echo @$_POST['firstname']; ?>" /><?php echo @$msg1; ?><br />
Surname: <input type="text" name="surname" value="<?php echo @$_POST['surname']; ?>" /><?php echo @$msg2; ?><br />
<input type="submit" name="submit" value="check!" />
</form>
<?php else: ?>
<b>Form has been submitted!</b>
<?php endif; ?>


</body>
</html>

really really thanks , your code even taught me some new functions .

but pop up some questions :

 

why you put && is_array($error) ? i deleted it , and nothing changed ?

what these @ means ? i know it prevent the php show error , but why they are needed there?

why you put && is_array($error) ? i deleted it , and nothing changed ?

I did a bit of datatype verification. If a variables is supposed to be an array before I use it I always check to make sure it is an array.

 

what these @ means ? i know it prevent the php show error , but why they are needed there?

@ symbols do just that prevent php error messages from appearing. I usually don't even use the @ symbol in any of my code however I did in this case I was being a bit lazy :D

i can't understand this part completely , 50 - 50

 

<?php if($show_form): ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Firstname: <input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" /><?php echo @$msg1; ?><br />
Surname: <input type="text" name="surname" value="<?php echo @$_POST['surname']; ?>" /><?php echo @$msg2; ?><br />
<input type="submit" name="submit" value="check!" />
</form>
<?php else: ?>
<b>Form has been submitted!</b>
<?php endif; ?>

 

 

some parts like this : <?php if($show_form): ?>

 

it is creating a php code within this php script ?

 

Its an if statement. With PHP you can go in 'n' out of PHP however many times you want. The above quoted code is the same as:

<?php if($show_form)
{
    echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
Firstname: <input type="text" name="firstname" value="' .@$_POST['firstname']. '" />' .@$msg1 . '><br />
Surname: <input type="text" name="surname" value=""' .@$_POST['surname']. '" />' .$msg2 .'<br />
<input type="submit" name="submit" value="check!" />
</form>';
}
else
{
    echo '<b>Form has been submitted!</b>';
} ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.