0

I've a html form with multiple select field. I'm trying to validate it with php. but i can't validate this multiple select field with php. It's show me success message without any validation.

Please kindly tell me what's the problem in my code. Thank you.

Php Code:

<?php
if(isset($_POST['Submit']) && $_POST['Submit'] == "Send SMS")
{

    if(isset($_POST['number']))
        $number = $_POST['number'];

    $msg = inputvalid($_POST['txt']);
    $err = array();

    if(isset($msg) && isset($number)) 
    {
        if(empty($msg) && empty($number))
            $err[] = "All field require";
        else
        {
            if(empty($msg)) 
                $err[] = "Your message require";
            if(empty($number))  
                $err[] = "Select your mobile number";
        }
    }   

    if(!empty($err))
    {
        echo "<div class='error'>"; 
        foreach($err as $er)
        {
            echo "<font color=red>$er.</font><br/>";                
        }
        echo "</div>";
        echo "<br/>";
    }
    else
    {
        echo "good";
    }
}
?>  

Html Code:

<form name="frm" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>">  
    <table width="800" border="0" cellspacing="10" cellpadding="0">
        <tr>
            <td valign="top">Number</td>
            <td>
                <select multiple="multiple" size="10" name="number[]">  
                    <option value="">--Select Member--</option>
                    <?php
                    $class = mysql_query("SELECT * FROM e_members");
                    while($res =  mysql_fetch_array($class))
                    {
                        $phone = $res['phone'];
                    ?>
                    <option value="<?php echo $phone; ?>"> <?php echo $phone; ?></option>
                    <?php
                    }
                    ?>
                </select>
            </td>
        </tr> 
        <tr>
            <td valign="top">Write message</td>
            <td>
                <textarea class="textarea" placeholder="Your message" name="txt" onkeyup="counter(this);">
                    <?php if(isset($_POST['txt'])) echo $_POST['txt']; ?>
                </textarea>
                <br/>
                <input type="" name="lbl" style="border:none;">
                <br/>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
                <input type="submit" name="Submit2" value="Save SMS" class="view"/>
                <input type="submit" name="Submit" value="Send SMS" class="submit"/>
            </td>
        </tr>
    </table>
</form>

Update:

After select multiple/single value var_dump showing:

array(1) { [0]=> string(13) "8801814758545" } 

Without select it's showing:

NULL
Alex
  • 71
  • 6

2 Answers2

0

You are trying to validate array $number using empty($number). It won't work as you expected

You can validate this as if (is_array($number) && count($number) > 0)

Shafeeque
  • 2,039
  • 2
  • 13
  • 28
0

The problem is that your check for empty values is inside for check for if(isset($msg) && isset($number)) and as soon as you post the form these variables are set. As you are already check that the post is set then remove this outer if statement and just check for empty values and it should work.

The Baker
  • 190
  • 3
  • 11