cybernet Posted September 10, 2012 Share Posted September 10, 2012 1st of all i'm sorry for the ironic title i would've name it " i need help with this $statement ", the only problem is that i don't know the name cause i would've so let's cut to the chase i have this if and else statement $status = ''; if ( !isset($_POST[ $x ]) || empty($message) || empty($email) ) $status = $lang['error']; else { $status = $lang['success']; mail($to, $subject, $body, $headers); } the i have this $no_p = ''; $yes_p = "<p style='color:red'>".$status."<br /></p>"; $show_status = (isset($status)) ? $no_p : $yes_p; // this is the function i don't know the name of it // this ? and : sign; what i'm trying to do is 1st of all if and else statement are executed only if the html form is submitted due to a swich case function in the html part i have the $show_status variable which i want to show NULL if the form wasn't submitted and if the form was submitted i want that $show_status to show either $lang['error'] OR $lang['success'] this is the only function i know to do this job, but the problem is that i got stuck this is how i arranged the code switch ($pageID) { case 'submit': foreach( array('lastname','firstname','email', 'message', 'phone') as $x ) { ${$x} = $_POST[ $x ]; } if ( !isset($_POST[ $x ]) || empty($message) || empty($email) ) $status = $lang['error']; else { $status = $lang['success']; mail($to, $subject, $body, $headers); } $no_p = ''; $yes_p = "<p style='color:red'>".$status."<br /></p>"; $show_status = (isset($status)) ? $no_p : $yes_p; break; } Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/ Share on other sites More sharing options...
Jessica Posted September 10, 2012 Share Posted September 10, 2012 It's called the ternary operator. Was that the only question? If not, you need to post more details other than "I'm stuck". Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376668 Share on other sites More sharing options...
cybernet Posted September 10, 2012 Author Share Posted September 10, 2012 in the html part i have the $show_status variable which i want to show NULL if the form wasn't submitted and if the form was submitted i want that $show_status to show either $lang['error'] OR $lang['success'] but $show_status shows NULL when executed Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376680 Share on other sites More sharing options...
scootstah Posted September 10, 2012 Share Posted September 10, 2012 foreach( array('lastname','firstname','email', 'message', 'phone') as $x ) { ${$x} = $_POST[ $x ]; } if ( !isset($_POST[ $x ]) || empty($message) || empty($email) ) This makes very little sense. What are you trying to do here? Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376681 Share on other sites More sharing options...
cybernet Posted September 10, 2012 Author Share Posted September 10, 2012 foreach( array('lastname','firstname','email', 'message', 'phone') as $x ) { ${$x} = $_POST[ $x ]; } if ( !isset($_POST[ $x ]) || empty($message) || empty($email) ) This makes very little sense. What are you trying to do here? for each value in the $array is created, $message $email etc ... // i guess you already knew this if the form isn't posted or message field is empty .. do that what i'm i missing ? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376688 Share on other sites More sharing options...
scootstah Posted September 10, 2012 Share Posted September 10, 2012 if ( !isset($_POST[ $x ]) This is equivalent to saying: if ( !isset($_POST['phone']), since "phone" was the last array item in the loop. Is that what you expected? Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376694 Share on other sites More sharing options...
cybernet Posted September 10, 2012 Author Share Posted September 10, 2012 if ( !isset($_POST[ $x ]) This is equivalent to saying: if ( !isset($_POST['phone']), since "phone" was the last array item in the loop. Is that what you expected? i know ... what i'm trying to do is make $show_status to either show NULL if the form wasn't submitted, if form submitted show either $lang['error'] OR $lang['success'] Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376699 Share on other sites More sharing options...
scootstah Posted September 10, 2012 Share Posted September 10, 2012 Well, you have seemingly no logic to determine if the form was submitted or not. Presumably you want something like: $show_status = null; $no_p = ''; $yes_p = "<p style='color:red'>".$status."<br /></p>"; // check if form is submitted if (!empty($_POST)) { $show_status = isset($status) ? $no_p : $yes_p; } Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376705 Share on other sites More sharing options...
cybernet Posted September 10, 2012 Author Share Posted September 10, 2012 Well, you have seemingly no logic to determine if the form was submitted or not. i don't how to explain my self in order for you guys to understand me ... Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376709 Share on other sites More sharing options...
lemmin Posted September 10, 2012 Share Posted September 10, 2012 foreach( array('lastname','firstname','email', 'message', 'phone') as $x ) { ${$x} = $_POST[ $x ]; } This loop is setting variable variables the names of which correspond to those in the array. This loop also assumes that the POST array contains all five of those string names as indices. Those are the $email and $message variable blow. if ( !isset($_POST[ $x ]) || empty($message) || empty($email) ) $status = $lang['error']; else { $status = $lang['success']; mail($to, $subject, $body, $headers); } This if statement will always evaluate to true since $x isn't defined (in the code you've shown). The scope of the closest $x ended in the foreach loop above it. I'm actually not sure what this is supposed to accomplish. At first I assumed you wanted this inside the foreach loop, but I'm guessing you don't want to send an email for EVERY input that is set. My guess is that you want to check required fields. Assuming they are ALL required fields, you can change the code to the following: if ( empty($lastname) || empty($firstname) || empty($message) || empty($email) || empty($phone)) $status = $lang['error']; else { $status = $lang['success']; mail($to, $subject, $body, $headers); } As for your ternary statement, you are always going to get the value of $no_p for $show_status. This is because isset($status) will ALWAYS be true since it is set in both conditions of your if-else statement (assuming $lang['error'] and $lang['success'] exist). i want that $show_status to show either $lang['error'] OR $lang['success'] Since you want it to print something either way, you don't even need that logic. This: $no_p = ''; $yes_p = "<p style='color:red'>".$status."<br /></p>"; $show_status = (isset($status)) ? $no_p : $yes_p; Could just be this: $yes_p = "<p style='color:red'>".$status."<br /></p>"; echo $yes_p; Or however you might be outputting it. As for returning NULL if the form wasn't submitted, as scootstah said, you don't have any logic for that. With your current logic, it will give the error message if it wasn't submitted. You can check for specific post values if you wanted to change that. Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376754 Share on other sites More sharing options...
cybernet Posted September 10, 2012 Author Share Posted September 10, 2012 @lemmin thank you very much for explaining me with so much detail but, you see this is what i did in the first place // before coming up with that ? statement : see the code was $htmlout = "<div>a</div> lalalalala <p style='color:red'>".$status."<br /></p> lalala"; and then i realised if the form wasn't submitted i will get <p style='color:red'><br /></p> so that will be shown for no reason / so <p> that will be there for nothing cause its empty so that's why i chose that ternary operator if status didn't had a value - don't show <p> tag at all and if it did show the <p> tag with error or success message ... i hope that all of you, understood what's my malefic plan thanks for your time Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376858 Share on other sites More sharing options...
scootstah Posted September 11, 2012 Share Posted September 11, 2012 So what's the problem then? Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376880 Share on other sites More sharing options...
cybernet Posted September 11, 2012 Author Share Posted September 11, 2012 So what's the problem then? if status didn't had a value - don't show <p> tag at all and if it did show the <p> tag with error or success message ... doesn't work $show_status doesn't show anything Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376976 Share on other sites More sharing options...
Christian F. Posted September 11, 2012 Share Posted September 11, 2012 Which means that this isset($status) always return true. Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1376978 Share on other sites More sharing options...
cybernet Posted September 11, 2012 Author Share Posted September 11, 2012 Which means that this isset($status) always return true. so what should i use ? Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1377025 Share on other sites More sharing options...
lemmin Posted September 11, 2012 Share Posted September 11, 2012 I recommend cleaning up the logic in this, but this should work in your implementation: foreach( array('lastname','firstname','email', 'message', 'phone') as $x ) { if (!isset($_POST[$x])) { $submitted = false; break; } ${$x} = $_POST[$x]; $submitted = true; } if ($submitted) { if ( empty($lastname) || empty($firstname) || empty($message) || empty($email) || empty($phone)) $status = $lang['error']; else { $status = $lang['success']; mail($to, $subject, $body, $headers); } $yes_p = "<p style='color:red'>".$status."<br /></p>"; echo $yes_p; } There is a difference between a $_POST variable being empty and set. If a form is submitted with nothing entered, those indices are still created, but are, in fact, empty(). You want to check for the existence of the indices before checking what value they hold, as I roughly accomplished above. If you know the indices exist, you know the form was submitted and you can check for validation. If the form wasn't submitted, it won't even go through the validation logic, as it shouldn't. Quote Link to comment https://forums.phpfreaks.com/topic/268215-need-help-else-please-take-a-look-thank-you/#findComment-1377028 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.