Avatar of code_red
code_red
Flag for Afghanistan asked on

PHP if statement...

I have a form that can potentially have a field value of "None".  When that happens, I want the value to go to null.  I was thinking an "if" statement, but all I can find is syntax for if/else, or if/echo (or print).  I really just need if/'change the value to nothing'.

I hope you can help!

code_red
PHP

Avatar of undefined
Last Comment
code_red

8/22/2022 - Mon
Ray Paseur

If you mean the value can be the text string "None" you can test for this and set the value to NULL.  Example:

if ($_POST['thing'] == 'None') $_POST['thing'] = NULL;

Does that make sense?
Tobias

Dear,

This code should do the work :


<?php
If($value=="None")
{
$value = NULL;
}
?>

Open in new window


Regards
code_red

ASKER
Thank you both! I cannot get either of these to work.  Where should I place the code?  I've tried both on the form and the formmail script.  They both seem they should do the trick though.

Editing to post my code...

Form page has an array, 4 classes for each student.  Here is the code for the array...

<?php
$Student1Classes = array('1Class1', '1Class2', '1Class3', '1Class4');
$Student1Courses = implode(", ", $Student1Classes);
?>

If no classes are picked, the value of $Student1Classes is "None, None, None, None".  I'd like for nothing to show if that is the case.

Thank you!
code_red
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Ray Paseur

Please post the part of the form page that creates the input statements in question.  I will add the action script code to show you how to process it.
Tobias

Dear,


foreach ($Student1Classes as &$classes) {
If($classes=="None")
{
$Student1Classes [$classes] = NULL; 
}

}

Open in new window


Regards

//Corrected forgot to update in the array
code_red

ASKER
Ray, I hope this is what you are asking for...and thank you.

          <select name="Student1Classes[]" id="1Class1">
            <option selected>None</option>
            <option selected>Math</option>
            <option selected>Science</option>
           </select>

          <select name="Student1Classes[]" id="1Class2">
            <option selected>None</option>
            <option selected>English</option>
            <option selected>Spanish</option>
           </select>

<?php
$Student1Classes = array('1Class1', '1Class2');
$Student1Courses = implode(", ", $Student1Classes);
?>

If nothing is selected, it posts, "None, None".  I'd just like a blank value if that is the case.

code_red
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

That's great.  I'll be back in few moments.
Ray Paseur

Experiment with this a little bit, then post back if you still have questions.
http://www.laprbass.com/RAY_temp_code_red.php

HTH, ~Ray
<?php // RAY_temp_code_red.php
error_reporting(E_ALL);
echo "<pre>";

// IF THE FORM HAS BEEN SUBMITTED
if (!empty($_POST))
{
    // ACTIVATE THIS TO SHOW THE POSTED DATA
    // var_dump($_POST);

    // SANITIZE AND PROCESS THE ARRAY
    $chosen = array();
    foreach ($_POST['Student1Classes'] as $class)
    {
        if (empty($class)) continue;
        $chosen[] = $class;
    }
    $classes = implode(',', $chosen);

    // SHOW THE WORK PRODUCT
    echo PHP_EOL;
    echo "YOU CHOSE: $classes";
}

// USE HEREDOC NOTATION TO CREATE THE HTML FORM
$form = <<<FORM
<form method="post">
<select name="Student1Classes[]" id="1Class1">
<option value="" selected>None</option>
<option value="Math">Math</option>
<option value="Science">Science</option>
</select>

<select name="Student1Classes[]" id="1Class2">
<option value="" selected>None</option>
<option value="EN">English</option>
<option value="ES">Spanish</option>
</select>

<input type="submit" />
</form>
FORM;

echo $form;

Open in new window

code_red

ASKER
This is great, the only thing is that my form results are sent via email, not just posted back on the same page.  How could I modify this to make that happen?

code_red
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Ray Paseur

Collect the results into a variable and use the variable in the construction of the email message.  See line 18 where we collect the results into a variable.
code_red

ASKER
I got it to work, but this still seems to submit the results as "None, None", if nothing is chosen.  I'd like it to submit an empty variable if nothing is chosen.  Does that make sense?
Ray Paseur

The objective makes sense, but if you have a script that is not producing the output you want, you will need to modify the script.  You might need to modify the HTML form (I did).
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
code_red

ASKER
Ok, let me try this...I have to have the value= "None" for the ones they don't choose since they are for different class periods.  That way, if they don't have a class chosen for 1st period, but do have one for 2nd, then it will look like: "None, Spanish".  Not: "Spanish".

Otherwise, this works just like my previous code.

I'm going to try to re-word my initial request here:  

I have a form that can potentially have a field value (through an array) of "None, None".  When that happens, I want the value to be deleted.  If the value from the array is "None, Spanish", or "Math, None", I want to keep it.  I was thinking an "if" statement, but all I can find is syntax for if/else, or if/echo (or print).  I really just need if value="None, None"/'change the value to nothing'.

<code>:
          <select name="Student1Classes[]" id="1Class1">
            <option selected>None</option>
            <option selected>Math</option>
            <option selected>Science</option>
           </select>

          <select name="Student1Classes[]" id="1Class2">
            <option selected>None</option>
            <option selected>English</option>
            <option selected>Spanish</option>
           </select>

<?php
$Student1Classes = array('1Class1', '1Class2');
$Student1Courses = implode(", ", $Student1Classes);
?>
</code>

If no option is chosen, this sends to email like this: Student1Classes: None, None

(Of course, the automatically generated letter that my php script (that does the mailing) calls for $Student1Classes, but shows:
 Array
 Array
- totally different issue, so probably not relevant here, but in case it was relevant I posted it here too)

I apologize that I'm not very clear, as I've been inundated in this since yesterday morning to no avail.  I hope that clears up (at least a little better) what I am needing here.  Thank you for all your help!

code_red
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
code_red

ASKER
Thank you, Ray. I was still unable to get this to do anything different than what my original code was already doing.  It's probably me.  I've been working with PHP for several years, but I don't work with it often.  Thank you for the help and the link!  I'll award the points since you did give me very usable code, just not exactly what I needed (again, I'm sure it's me).  Although I'm sure it will be very useful for others.

code_red
Ray Paseur

Thanks for the points.  I can't really write your code for you, but hopefully the design pattern is easy enough to follow that you can see the moving parts and implement them in your application.  Best of luck with it, ~Ray
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
code_red

ASKER
Ok, here is what I ended up doing...
On the formmail page (after form is submitted - this page holds the code sends the email):
<?php
$Student1Classes = implode(", ", $_POST["Student1Classes"]);
if($Student1Classes=="None, None")
{
$Student1Classes = NULL;
}
?>

What this does is still send the email with the form results to me as "None, None", BUT the letter that is also generated to go to the user (on the same page as the formmail code - and the above code), DOES in fact go NULL.  This will definitely suffice.  

Thank you for leading me in the right direction.