Link to home
Start Free TrialLog in
Avatar of code_red
code_redFlag 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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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?
Avatar of Tobias
Dear,

This code should do the work :


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

Open in new window


Regards
Avatar of 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
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.
Dear,


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

}

Open in new window


Regards

//Corrected forgot to update in the array
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
That's great.  I'll be back in few moments.
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

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
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.
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?
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).
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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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.