If statement problems..

Ok I’m not great at explaining things so here goes. :slight_smile: There is one section of our customer contact form that I’m having problems with. (I’ve cut out all the other pieces of validation just to zoom in on this section for readability.)

There is an input field for phone number and beside it a check box if the customer wants to request a call back. When this is sent to email it will mean there are three options for the variable $phone_me this will replace the echo statements that are there now.

Any way my problem, I have tried so many different ways of doing this and I get the outcome I want but I keep getting the error Undefined index: call which is related to where the variables have been set. Just before the if statement. Where $call = $_POST[‘call’]; is set to the variable $call. It’s driving me loopy to be honest. I’ve been at it all day. I’m new to php, so if I have made a classic school boy error would someone be kind enough to point it out. Or if there is a better way of writing this, if you wouldn’t mind showing me an example I would be ever so grateful.

Many many thanks in advance for any advice and info given,
Oraya

[php]

<?php //IF FORM NOT YET SUBMITTED //DISPLAY THE FORM if (!isset($_POST['submit'])) { ?>

[/php]

<form name="ContactUs" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<fieldset class="form_fieldset">
<legend>&nbsp;Contact Us&nbsp;</legend>

<div><label class="mail_label">Title:</label> 
<select name="title" class="select" id="required">
<option></option>
<optgroup label="--- Title ---">
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
</optgroup>
</select></div>

<div>
<label class="mail_label">First Name:</label>
<input type="text" name="first_name" value="" class="mail" id="required"/>
</div>

<div>
<label class="mail_label">Last Name:</label>
<input type="text" name="last_name" value="" class="mail" id="required"/>
</div>

<div>
<label class="mail_label">Email:</label> 
<input type="text" name="email" value="" class="mail" id="required"/>
<div class="mail_checkbox">
<input type="checkbox" name="call" value="Yes" /> Do you wish us to call you?:
</div></div>

<div>
<label class="mail_label">Phone:</label>
<input type="text" name="phone" value="" class="mail" id="not_required"/>
</div>

<div>
<label class="mail_label">Subject:</label>
<select name="subject" class="subject-select" id="required">
<option></option>
<optgroup label="--- Subject ---">
<option>Report Website Issuses</option>
<option>Enquiries</option>
<option>Other</option>
</optgroup>
</select>
</div>

<label class="mail_label">Message:</label>
<br />
<textarea wrap="physical" id="form_textarea" name="message" class="mail"></textarea>

<div class="warning"><strong>Please Note:</strong> Required fields are highlighted in <ins>Blue</ins>!</div>

<br />
<div class="contact_submit"><input type="submit" name="submit" value=" Contact Us " class="form_submit"/>&nbsp;<input type="reset" value="Clear Form" class="form_submit"/></div>

</fieldset>
</form>

[php]

<?php // IF FORM SUBMITTED // PROCESS FORM THE DATA INPUT } else { // SET THE VARIABLES. $phone = $_POST['phone']; $call = $_POST['call']; if(strlen($phone) ==0) { echo "Do nothing"; } elseif ($call == "Yes") { echo "Customere left phone number, and requested call back. Their phone number is: "; echo $_POST['phone']; } else { echo "Phone left their phone number but not request a call back: Their phone numnber is: "; echo $_POST['phone']; } }?>

[/php]

Those aren’t anything serious, its just telling you its a variable with no value. If everything else is working, just turn off error reporting or surpress the messages by doing $call = @$_POST[‘call’];

Please don’t hide errors - they tend to be there for a reason. Relying on the ability to hide errors just leads to bad code. Simply use an if statement (first block of code) or the more compact ‘tertiary’ equivalent (second block of code).

[php]// Full if statements
$phone = ‘’;
$call = ‘’;
if(isset($_POST[‘phone’]))
{
$phone = trim($_POST[‘phone’]);
}
if(isset($_POST[‘call’]))
{
$call = trim($_POST[‘call’]);
}

//or

$phone = ‘’;
$call = ‘’;
if(isset($_POST[‘phone’])) $phone = trim($_POST[‘phone’]);
if(isset($_POST[‘call’])) $call = trim($_POST[‘call’]);[/php]

[php]//Tertiary operator method
$phone = isset($_POST[‘phone’]) ? trim($_POST[‘phone’]) : ‘’;
$call = isset($_POST[‘call’]) ? trim($_POST[‘call’]) : ‘’;
//variable = (statement) ? (value if true) : (value if false)[/php]

It might be a bit more code, but you should find it worth the time.

Those aren’t errors though, at least not in the typical sense. its just the code telling you its finding a variable that hasn’t been used. in this case, its ok to surpress it. if(isset()) can have some unintended results too.

That’s a very real error. You are trying to set a variable to another variable that doesn’t exist, therefore relying on the code to ignore this lack of care and simply pick a random value out of the air (typically a blank string).

In regards to the comment about the isset() function, in my years of coding and unimaginable uses of this function, never has it given an unintended result, nor will it (unless used improperly).

If having to turn off error reporting to have your code run (seemingly) well suits you, then fine, please continue to do so. However, PHP wouldn’t report an error if it wasn’t something to take notice of - plus it is much more satisfying to be able to run thousands of lines of code without a single error, notice, or warning (all because of paying attention to coding standards).

Back to the OP - as richei mentioned, you can indeed turn off error reporting and simply hide the error. That will stop the error being output onto the page. It will not stop the error occurring however - the fix for which is in my post.

The choice is ultimately yours oraya, and apologies for this slightly off-topic discussion between richei and myself on your thread.

Sponsor our Newsletter | Privacy Policy | Terms of Service