Jump to content

need help ? else please take a look : thank you


cybernet

Recommended Posts

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  :rtfm:

 

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 :shrug:

 

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;
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ? :-\

Link to comment
Share on other sites

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']

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

	
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.

Link to comment
Share on other sites

@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 :D

thanks for your time

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.