asmith Posted November 24, 2007 Share Posted November 24, 2007 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/78684-solved-why-this-code-do-not-work/ Share on other sites More sharing options...
wildteen88 Posted November 24, 2007 Share Posted November 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/78684-solved-why-this-code-do-not-work/#findComment-398198 Share on other sites More sharing options...
asmith Posted November 24, 2007 Author Share Posted November 24, 2007 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 ! ??? :'( Quote Link to comment https://forums.phpfreaks.com/topic/78684-solved-why-this-code-do-not-work/#findComment-398209 Share on other sites More sharing options...
wildteen88 Posted November 24, 2007 Share Posted November 24, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/78684-solved-why-this-code-do-not-work/#findComment-398220 Share on other sites More sharing options...
asmith Posted November 24, 2007 Author Share Posted November 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/78684-solved-why-this-code-do-not-work/#findComment-398246 Share on other sites More sharing options...
wildteen88 Posted November 24, 2007 Share Posted November 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78684-solved-why-this-code-do-not-work/#findComment-398252 Share on other sites More sharing options...
asmith Posted November 24, 2007 Author Share Posted November 24, 2007 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/78684-solved-why-this-code-do-not-work/#findComment-398255 Share on other sites More sharing options...
wildteen88 Posted November 24, 2007 Share Posted November 24, 2007 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78684-solved-why-this-code-do-not-work/#findComment-398260 Share on other sites More sharing options...
asmith Posted November 24, 2007 Author Share Posted November 24, 2007 nice !!!!! 1.and that endif closes all ? 2.phew the last thing ! what exactlly extract do ? php.net says : extract : Import variables into the current symbol table from an array , but i can't quiet its job... Quote Link to comment https://forums.phpfreaks.com/topic/78684-solved-why-this-code-do-not-work/#findComment-398262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.