Page 1 of 1

Dropdown blank selection

Posted: Mon Feb 11, 2008 8:11 pm
by ttleser
I've downloaded a contact us form from a site (Free-php-Scripts.net) and can't understand how to get a blank entry or "Please Choose" entry at the top of a dropdown list that won't be considered a valid entry for emailing if the user didn't change it. Help?

Here's the php file that contains the form code:

Code: Select all

<?php
/*********************************************************
        This Free Script was downloaded at          
        Free-php-Scripts.net (HelpPHP.net)          
    This script is produced under the LGPL license      
        Which is included with your download.           
    Not like you are going to read it, but it mostly    
    States that you are free to do whatever you want    
            With this script!                       
        NOTE: Linkback is not required, 
    but its more of a show of appreciation to us.                   
*********************************************************/
 
//Include configuration file and function file
//(default location is in the same directory)
include_once('contact_form/config.php');
include_once('contact_form/functions.php');
 
//If contact is being sent:
if($_POST['submit_id'] == 1){
    //Check name entered
    if($_POST['name'] == NULL){ $message = 'Please enter your name.';}
    
    //check if email is enetered
    if($message == NULL && is_valid_email($_POST['email']) == false ){ $message = 'Please enter a valid email.';}
    
    //check if message is entered
    if($_POST['message_text'] == NULL && $message == NULL){ $message = 'Please enter a comment.';}  
    
    //File Upload checks
    if($message == NULL && $FILE_UPLOAD == 1 && $_FILES['user_file']['name'] != NULL){
        if($_FILES['user_file']['size'] > (($FILE_UPLOAD_MAX*1024)*1024)){ $message = 'File is over '.$FILE_UPLOAD_MAX.' MB in size.';}
        if($message == NULL && allowed_ext($FILE_UPLOADS_EXT,$_FILES['user_file']['name']) == false){$message = 'Invalid extension.';}
        $new_filename = date("G_i_s_").$_FILES['user_file']['name'];
    }
    
    //Image verificaiton checks
    if($message == NULL && $IMAGE_VERIFICATION == 1){
        $te_co = hex2bin($_POST['hid_code']);
        $word_is = RC4($te_co,$IMAGE_VER_CODE);
        if($word_is != $_POST['confirm_image']){$message = 'Your verfication code is incorrect.';}
    }
    //End verifications, start processing
    if($message == NULL){
        //Check if file upload is needed
        if($FILE_UPLOAD == 1 && $_FILES['user_file']['name'] != NULL){
            //Store file for keep and email
            move_uploaded_file($_FILES['user_file']['tmp_name'],$FILE_UPLOADS_DIR.$new_filename);
        }
        //compose admin/user message templates replaces
        $do_search = array('$+name+$','$+email+$','$+message_text+$','$+reason+$');
        $do_replace = array($_POST['name'],$_POST['email'],$_POST['message_text'],$_POST['reason']);
                
        
        //Send user email?
        if($SEND_THANKS == 1){
            $user_message = str_replace($do_search,$do_replace,$USER_TEMPLATE);
            //Set Headers
            $user_header = "Return-Path: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n"; 
            $user_header .= "From: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n";
            $user_header .= "Content-Type: ".$EMAIL_OPTIONS['TYPE']."; charset=".$EMAIL_OPTIONS['CHARSET'].";\n\n\r\n"; 
            //Send Thank you
            mail ($_POST['email'],'Your recent Herter Music Center inquiry',$user_message,$user_header);    
        }
        
        //Send admi email?
        if(count($ADMIN_EMAILS) > 0){
            $admin_message = str_replace($do_search,$do_replace,$ADMIN_TEMPLATE);
            //Do we need to send file as attachment?
            if($FILE_DO != 1){
                //Get file attriubtes
                $fileatt_type = $_FILES['user_file']['type'];
                
                $file = fopen($FILE_UPLOADS_DIR.$new_filename,'rb');
                while($dat = fread($file,1025657)){
                    $attachment_data .= $dat;
                }
                fclose($file);
                
                // Encode file content
                $attachment_data = chunk_split(base64_encode($attachment_data));
                //File upload headers
                $semi_rand = md5(time());
                $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
                
                $headers = "From: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">";
                
                // Add the headers for a file attachment
                $headers .= "\nMIME-Version: 1.0\n" .
                    "Content-Type: multipart/mixed;\n" .
                    " boundary=\"{$mime_boundary}\"";                       
                
                  // Add a multipart boundary above the plain message
                $new_message = "This is a multi-part message in MIME format.\n\n" .
                    "--{$mime_boundary}\n" .
                    "Content-Type: ".$EMAIL_OPTIONS['TYPE']."; charset=\"".$EMAIL_OPTIONS['CHARSET']."\"\n" .
                    "Content-Transfer-Encoding: 7bit\n\n" .
                    $admin_message . "\n\n";
                        
                  // Add file attachment to the message
                 $new_message .= "--{$mime_boundary}\n" .
                     "Content-Type: {$fileatt_type};\n" .
                     " name=\"{$new_filename}\"\n" .
                     "Content-Disposition: attachment;\n" .
                     " filename=\"{$new_filename}\"\n" .
                     "Content-Transfer-Encoding: base64\n\n" .
                     $attachment_data . "\n\n" .
                     "--{$mime_boundary}--\n"; 
                
                unset($attachment_data);
            } else {
                //regular headers
                $headers = "Return-Path: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n"; 
                $headers .= "From: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n";
                $headers .= "Content-Type: ".$EMAIL_OPTIONS['TYPE']."; charset=".$EMAIL_OPTIONS['CHARSET'].";\n\n\r\n"; 
                $new_message = $admin_message;
            }
            //Send admin emails
            if ($_POST['reason'] == 'Band Camp') { 
            mail ($bandcamp,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);
            } elseif ($_POST['reason'] == 'Billing') { 
            mail ($accounting,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);
            } elseif ($_POST['reason'] == 'General Information') { 
            mail ($generalinfo,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);
            } elseif ($_POST['reason'] == 'Instrument Repair') { 
            mail ($repair,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);    
            } elseif ($_POST['reason'] == 'Keyboard Information') { 
            mail ($keyboardinfo,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);
            } elseif ($_POST['reason'] == 'MusicLessonsBayCity') { 
            mail ($musiclessonsbaycity,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);
            } elseif ($_POST['reason'] == 'MusicLessonsSaginaw') { 
            mail ($musiclessonssaginaw,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);
            } elseif ($_POST['reason'] == 'Product Information') { 
            mail ($productinfo,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);
            } elseif ($_POST['reason'] == 'School Services') { 
            mail ($schoolservices,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);    
            } elseif ($_POST['reason'] == 'SheetMusic') { 
            mail ($sheetmusic,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);
            } elseif ($_POST['reason'] == 'Other') { 
            mail ($other,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers); 
            } elseif ($_POST['reason'] == 'Webmaster') { 
            mail ('ttleser@gauntletgaming.com',$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);
}
 
//          foreach($ADMIN_EMAILS as $this_email){
//              mail ($this_email,$EMAIL_OPTIONS['USER_SUBJECT'],$new_message,$headers);    
//          }
        }
        
        //Remove file if not needed
        if($FILE_DO == 2){
            unlink($FILE_UPLOADS_DIR.$new_filename);
        }
        $message = 'Your question has been sent, thank you. We will contact you shortly.';
        $_POST = NULL;
    }
}
if($message != NULL){
?>
<table width="100%"  border="0" cellpadding="5" cellspacing="0" bgcolor="#FF8080">
  <tr>
    <td bgcolor="#FFD5D5"><font color="#FF0000"><?=$message;?></font></td>
  </tr>
</table>
<br/>
<?php } ?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data" name="contact" id="contact" style="display:inline;">
<table width="100%"  border="0" align="left" cellpadding="5" cellspacing="0">
    <tr>
      <td>Name:</td>
        <td><input name="name" type="text" id="name" style="width:250px;" value="<?php echo $_POST['name'];?>"></td>
    </tr>
    <tr>
      <td>Email:</td>
        <td><input name="email" type="text" id="email" style="width:250px;" value="<?php echo $_POST['email'];?>"></td>
    </tr>
    <tr>
      <td>Reason for contact: </td>
        <td><select name="reason" id="reason" style="width:250px;">
        <?php if($_POST['reason'] == 'Band Camp' || $_POST['reason'] == NULL){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="Band Camp"<?=$sel;?>>Band Camp</option>
        <?php if($_POST['reason'] == 'Billing'){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="Billing"<?=$sel;?>>Billing</option>
        <?php if($_POST['reason'] == 'General Information'){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="General Information"<?=$sel;?>>General Information</option>
        <?php if($_POST['reason'] == 'Instrument Repair'){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="Instrument Repair"<?=$sel;?>>Instrument Repair</option>
        <?php if($_POST['reason'] == 'Keyboard Information'){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="Keyboard Information"<?=$sel;?>>Keyboard Information</option>
        <?php if($_POST['reason'] == 'MusicLessonsBayCity'){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="MusicLessonsBayCity"<?=$sel;?>>Music Lessons - Bay City</option>
        <?php if($_POST['reason'] == 'MusicLessonsSaginaw'){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="MusicLessonsSaginaw"<?=$sel;?>>Music Lessons - Saginaw</option>
        <?php if($_POST['reason'] == 'Product Information'){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="Product Information"<?=$sel;?>>Product Information</option>
        <?php if($_POST['reason'] == 'School Services'){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="School Services"<?=$sel;?>>School Services</option>
        <?php if($_POST['reason'] == 'Sheet Music'){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="Sheet Music"<?=$sel;?>>Sheet Music</option>
        <?php if($_POST['reason'] == 'WebMaster'){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="Webmaster"<?=$sel;?>>Webmaster</option>
        <?php if($_POST['reason'] == 'Other'){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="Other<?=$sel;?>">Other</option>
      </select></td>
    </tr>
    <tr>    
      <td>Message:</td>
        <td><textarea name="message_text" cols="60" rows="6" id="message_text"><?php echo $_POST['message_text'];?></textarea></td>
    </tr>
    <?php
    if($IMAGE_VERIFICATION == 1){?>
    <tr>
      <td>Verification code:</td>
        <td>          <table  border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td><?php
                $referenceid = md5(mktime()*rand());
                //Generate the random string
                $chars = array("a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k",
                "K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v",
                "V","w","W","x","X","y","Y","z","Z","1","2","3","4","5","6","7","8","9");
                $length = 8;
                $textstr = "";
                for ($i=0; $i<$length; $i++) {
                   $textstr .= $chars[rand(0, count($chars)-1)];
                }
                $new_string = RC4($textstr,$IMAGE_VER_CODE);
                $image_link = bin2hex($new_string);
                ?>
                <img src="../contact_form/sec_image.php?code=<?=$image_link;?>">
                <input name="hid_code" type="hidden" id="hid_code" value="<?=$image_link;?>"><br/>
                <input name="confirm_image" type="text" id="confirm_image" value="<?php echo $_POST['confirm_image'];?>"></td>
            </tr>
            </table>
        </td>
    </tr>
    <?php }
    if($FILE_UPLOAD == 1){?>
    <tr>
      <td>File Upload </td>
      <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><input name="user_file" type="file" id="user_file"></td>
        </tr>
        <tr>
          <td><font color="#FF0000" size="2">Max. File Size: 
            <?=$FILE_UPLOAD_MAX;?> 
            MB, Allowed Extensions: 
            <?php 
            if($FILE_UPLOADS_EXT == 1){ echo 'All';}else{
            foreach($FILE_UPLOADS_EXT as $ext){ echo '-'.$ext;}}?>
          </font></td>
        </tr>
      </table>        </td>
    </tr>
    <?php } ?>
    <tr>
        <td>
            <td>
                <br>
                <input type="submit" name="Submit" value="Submit">
                <input name="submit_id" type="hidden" id="submit_id" value="1">
            </td>
        </td>
    </tr>
</table>     
</form>
<?php
/*
Copyright notice (this notice won't show to users but 
will help other coders who are interested in the
script to find us */
 
echo "<!--
/*********************************************************
                    Contact Form
            This Free Script was downloaded at          
              http://www.Free-php-Scripts.net 
*********************************************************/
-->";
 
/* +++++++++++++++++++++++++++++++++++++
        END Contact FILE
---------------------------------------*/
 
/*
Partner Sites:
====================
 
Free File Upload: 
            http://www.HotFile.us
            
Free Image Hosting: 
            http://www.MyImage.us
            
Free Games (all type of games): 
            http://www.FunTimes.us
            
Free Templates: 
            http://www.allfreetemplates.us
            
PHP Skills and Tricks: 
            http://www.phptricks.com
*/
?>
I'm guessing it'll be in this section:

Code: Select all

<td>Reason for contact: </td>
        <td><select name="reason" id="reason" style="width:250px;">
        <?php if($_POST['reason'] == 'Band Camp' || $_POST['reason'] == NULL){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="Band Camp"<?=$sel;?>>Band Camp</option>

Re: Dropdown blank selection

Posted: Thu Feb 14, 2008 9:10 pm
by ttleser
Bump?

Re: Dropdown blank selection

Posted: Fri Feb 15, 2008 12:52 pm
by califdon
ttleser wrote:I've downloaded a contact us form from a site (Free-php-Scripts.net) and can't understand how to get a blank entry or "Please Choose" entry at the top of a dropdown list that won't be considered a valid entry for emailing if the user didn't change it. Help?

I'm guessing it'll be in this section:

Code: Select all

<td>Reason for contact: </td>
        <td><select name="reason" id="reason" style="width:250px;">
        <?php if($_POST['reason'] == 'Band Camp' || $_POST['reason'] == NULL){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="Band Camp"<?=$sel;?>>Band Camp</option>
Your guess was correct.

You just need to insert your blank or "Please Choose" option as the first one, immediately after the <select...> line. Between the <option...> and </option> tags, either leave it blank or enter the phrase you want to appear. For the "value=" value, use whatever you want to test for in your script to determine whether it's a valid entry or not--perhaps "INVALID" or something. To make this appear as the default selection when the form is first displayed, add the keyword "selected" in the <option> tag, after the "value=".

Re: Dropdown blank selection

Posted: Fri Feb 15, 2008 1:01 pm
by RobertGonzalez
Generally speaking you want to set your default "novalue" value as either a 0 or -1. Then in your code you check against the known default value to see if something was set.

Re: Dropdown blank selection

Posted: Sat Feb 16, 2008 1:43 pm
by ttleser
Thanks for the replies.. I can't get this part working:

Code: Select all

<?php if($_POST['reason'] == '' || $_POST['reason'] == NULL){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="invalid" selected="selected"></option>
 
I added the above code right after the <select...> line like you said, but above the "bandcamp" line. The bandcamp selection still comes up as default though there is a blank line above the selection in the dropdown on the webpage.

Also, I added this in the beginning:

Code: Select all

if($_POST['reason'] == "invalid"){ $message = 'Please enter a reason.';}
That should be good enough if the reason isn't filled out correct? If not what do I need to change on either 2 lines above would correct this?

Re: Dropdown blank selection

Posted: Sat Feb 16, 2008 1:53 pm
by califdon
ttleser wrote:

Code: Select all

<?php if($_POST['reason'] == '' || $_POST['reason'] == NULL){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="invalid" [color=#FF0000]selected[/color]></option>
 
That will give you a blank first line in the dropdown box, if that's what you want.
Also, I added this in the beginning:

Code: Select all

if($_POST['reason'] == "invalid"){ $message = 'Please enter a reason.';}
That should be good enough if the reason isn't filled out correct? If not what do I need to change on either 2 lines above would correct this?
Well, that assigns a value to $message, but of course you have to then use that variable to notify the user in some way, and that depends on how you want to do it.

Re: Dropdown blank selection

Posted: Sat Feb 16, 2008 2:13 pm
by ttleser
Ok, got the page working. Here's the corrected lines of code.

Code: Select all

if($_POST['reason'] == "invalid"){ $message = 'Please enter a reason.';}
&

Code: Select all

<?php if($_POST['reason'] == '' || $_POST['reason'] == NULL){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="invalid" selected="selected"></option>
        <?php if($_POST['reason'] == 'Band Camp' || $_POST['reason'] == NULL){ $sel = ' selected';} else { $sel = NULL;} ?>
        <option value="Band Camp">Band Camp</option>
removed the <?=$sel;?> from the bandcamp line, though I left it in for the other options and the blank entry STILL shows up as default **Shrug**

Also, added this line to help tell the person when they submitted it where it went. ;)

Code: Select all

$message = 'Your '.$_POST['reason'].' question has been sent, thank you. We will contact you shortly.';


Now, for one last question. When the user submits the form, it'll say at the top "Your ... question has been sent..." , but the form will still be there for them to fill out again. I would like the page JUST to show the message and force them to click a link that'll take them either back to the form or to a different page. How might I do this?

Re: Dropdown blank selection

Posted: Sat Feb 16, 2008 2:44 pm
by califdon
ttleser wrote: Now, for one last question. When the user submits the form, it'll say at the top "Your ... question has been sent..." , but the form will still be there for them to fill out again. I would like the page JUST to show the message and force them to click a link that'll take them either back to the form or to a different page. How might I do this?
I'm glad that you got your page running, but now you're asking for someone to rewrite a lengthy script that you downloaded, in order to make it do something that it apparently wasn't originally written to do. If someone has time to do that for you, that's fine, but most of us are here to try to help someone with a programming question, not write their scripts for them. I hope I am not being rude, but I just wanted to explain that to you.

Re: Dropdown blank selection

Posted: Sat Feb 16, 2008 3:49 pm
by ttleser
Completely understandable, and no I didn't think you were rude.

I was hoping that it was a quick code fix in there that would tell it not to display the form again.. Hmm, if anyone can scan it over and show me a quick fix great, if not I'll keep hunting. :D

Re: Dropdown blank selection

Posted: Sat Feb 16, 2008 3:54 pm
by Benjamin
Without looking at the code, A quick fix would be to send a header redirect and then terminate execution after you have processed the form. You can only do this if the headers have not already been sent. An example is below:

Code: Select all

 
header("Location: http://domainname.com/thank_you.php");
exit();
 

Re: Dropdown blank selection

Posted: Sun Feb 17, 2008 1:34 pm
by RobertGonzalez
Here's some pseudo code for you to consider:

Code: Select all

<?php
// Set a form submission success flag and default it to false
// Set an error message variable to null
 
// if the the form was submitted
// if the form validates
// do what is needed with the form data and change the form submission success flag to true
// else set a small error message into the error message variable
 
// If the form submission success flag is a success
// show the success message
// else if there is an error message
// show the error message 
// show the form
?>
Now go code that.

Re: Dropdown blank selection

Posted: Mon Feb 18, 2008 5:57 pm
by ttleser
astions
Without looking at the code, A quick fix would be to send a header redirect and then terminate execution after you have processed the form. You can only do this if the headers have not already been sent. An example is below:
Hmm, I'm not that good with PHP. Do I need to put that code in a certain place? Is there a "flow" to the code for which the browser follows?


@Everah
I think I'll have to learn a little more PHP before I'm able to do that. Probably pretty easy but I still haven't quite figured out how php flows like normal program codes.

Re: Dropdown blank selection

Posted: Mon Feb 18, 2008 6:03 pm
by Benjamin
Do you know where in the code your done processing the form? That is where you would put the header redirect and exit code that I posted.