homepage Welcome to WebmasterWorld Guest from 54.145.183.169
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Need help with empty field error message
outdoorxtreme1

5+ Year Member



 
Msg#: 4664997 posted 1:47 pm on Apr 22, 2014 (gmt 0)

Hi, I'm a beginner to PHP scripting. I'm trying to get the error message to work correctly when the number field is left blank in this code. If you see anything else I may be doing wrong or a better way I would appreciate any input. basically what this script will do is that a person can enter their cell number and select their carrier then press submit. It will then send them a message to their moble. Thanks for your help.

<?php

echo"<form id=\"sms\" name=\"sms\" method=\"post\"

action=\"".$_SERVER['PHP_SELF']."\">
<table width=\"400\">
<tr>
<td align=\"right\" valign=\"top\">Cell number:</td>
<td align=\"left\"><input name=\"number\" type=\"text\" id=\"number\"

size=\"10\"> No dashes</td>
</tr>
<tr>
<td align=\"right\" valign=\"top\">Carrier:</td>
<td align=\"left\"><select name=\"carrier\" id=\"carrier\">
<option value=\"verizon\">Verizon</option>
<option value=\"tmobile\">T-Mobile</option>
<option value=\"sprint\">Sprint</option>
<option value=\"att\">AT&amp;T</option>
<option value=\"virgin\">Virgin Mobile</option>
</select></td>
</tr>
<tr>
<td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"Submit\"

value=\"Submit\"></td>
</tr>
</table>
</form>";


$number = $_POST['number'];
$carrier = $_POST['carrier'];
$message = "test";

if( !empty($_POST['number']))
echo "You must enter a number to send to";

else if ($carrier == "verizon") {
$formatted_number = $number."@vtext.com";
mail("$formatted_number", "", "$message", "From: john@gmail.com");

echo "Success";
}

else if ($carrier == "tmobile") {
$formatted_number = $number."@tomomail.net";
mail("$formatted_number", "", "$message", "From: john@gmail.com");

echo "Success";
}

else if ($carrier == "sprint") {
$formatted_number = $number."@messaging.sprintpcs.com";
mail("$formatted_number", "", "$message", "From: john@gmail.com");

}

else if ($carrier == "att") {
$formatted_number = $number."@txt.att.net";
mail("$formatted_number", "", "$message", "From: john@gmail.com");

echo "Success";
}

else if ($carrier == "virgin") {
$formatted_number = $number."@vmobl.com";
mail("$formatted_number", "", "$message", "From: john@gmail.com");

echo "Success";
}
?>

 

outdoorxtreme1

5+ Year Member



 
Msg#: 4664997 posted 3:15 pm on Apr 22, 2014 (gmt 0)

Currently, when a number is not entered and submit is pressed, it displays "Success" it should display "You must enter a number to send to". I only want the message displayed after the submit button is pressed.

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4664997 posted 8:05 am on Apr 24, 2014 (gmt 0)

if( !empty($_POST['number']))


This says "if 'number' is NOT empty then ...". You need to remove the exclamation mark (the negation operator) and since you are already assigning this to the $number variable, it should be something like:

if (empty($number))

But you also need to check that the form is actually submitted before attempting to process it. You appear to be validating the form input immediately on first page load when the form won't have been submitted...

if (isset($_POST['submit'])) {
/* Process form input */



echo"<form id=\"sms\" name=\"sms\" method=\"post\"


Instead of echo'ing this to the page, just output it directly (outside of your PHP tags). Also, avoid using double quotes to delimit strings that contain lots of double quotes. Use single quotes (or HEREDOC syntax) instead so you don't need to escape the quotes. eg.

echo '<form id="sms" name="sms" method="post"

But as mentioned above, this should be output directly anyway.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved