SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Feedback with validation problem?

    Arghh!!... Havin some problem with the code.. All is ok including the validation part ---> except that if i click the "Submit" button, it will generate an email even though the validation error is displayed.

    Got any pointer o' gurus out there?

    --------------------

    PHP Code:

    <?
    /* Declare some functions */

        
    function print_form($name$MailFromAddress) {
    ?>

    <form name="form1" method="post" action="test_contct.php">
    .                                
    ...
    ...
    <td height="26" colspan="2"><input name="submit" type="submit" id="submit2" value="Submit"><input type="reset" name="Submit2" value="Reset">
    </td>
    </tr>
    </table>


    <? }
    function 
    check_form($name$MailFromAddress) {

    if(!
    $name || !$MailFromAddress ):
    print(
    "<font type=verdana size=1 color=red>You are missing some required fields:<br></font><br>");

    if(!
    $name) {
    print(
    "<font type=verdana size=1 color=red>- You need to fill in your <b>Name</b>.<br></font>"); }

    if(!
    $MailFromAddress) {
    print(
    "<font type=verdana size=1 color=red>- You need to fill in your <b>Email</b>.<br></font>"); }
         
    print_form($name$MailFromAddress);
    else:

    confirm_form($name$MailFromAddress);
    endif;
            }
    ?>

    <?
    function confirm_form($name$MailFromAddress) {
    ?>

    <font type=verdana size=1><b><br><span class="dateBold">
    Have a pleasant day! 
    </span><span class="style2"><br></span></b> </font>  

    <span class="nameBold"><font type=verdana size=1><b>We thank you for your interest. </b></font></span><span class="style2"><font type=verdana size=1><b>.</b></font></span>
                                                
            
            
    <?    }    
    /* Begin Main Program */

    if(!$submit):
        
    ?>             
                             
    <?
        print_form
    ("","","","","","");
    else:
        
    check_form($name$MailFromAddress);
    endif;
    ?>
    <?  
    if(isset($submit)):

        
    $MailToAddress "me@myemailcom";

        
    $MailSubject "Enquiries Page - $select";
        if (!
    $MailFromAddress) {
        
    $MailFromAddress "$MailFromAddress";
        }
        
    $ip =  $HTTP_SERVER_VARS['REMOTE_ADDR'] ;
        
    $date date("d/m/Y H:i:s");
    ?>
    <?
        reset
    ($HTTP_POST_VARS);
     
    $Message .= "Sender Name : $name\n\n"
     
    $Message .= "Sender Email: $MailFromAddress \n\n"
     
    $Message .= "Sender Contact No.: $contact \n\n"
     
    $Message .= "Sender IP address : $ip \n\n";
     
    $Message .= "Sender Date and Time : $date \n\n";
     
    $Message .= "Feedback : \n";
     
    $Message .= "$feedback\n";

    mail("$MailToAddress""$MailSubject""$Message","From: $MailFromAddress); 
        

    else:

    endif ;
    ?>

    </form>
    -----------------

  2. #2
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mail() is always called if isset($submit) is true. you need to only call it if you validation returns true. Perhaps if you actually put the mail part in your validation function just before confirm_form(), that way it will only be called if the validation succeeds.

  3. #3
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Where is your $submit variable coming from ?
    +
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  4. #4
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gavwvin
    mail() is always called if isset($submit) is true. you need to only call it if you validation returns true. Perhaps if you actually put the mail part in your validation function just before confirm_form(), that way it will only be called if the validation succeeds.

    ---> U mean I take out the whole chunk from

    ---------------

    PHP Code:
    <?  
    if(isset($submit)): 
    .
    ..
    .
    endif ;
    ?>
    -----------

    and insert it betwn

    -----

    PHP Code:
    .
    ..
    ?>
                          <------------------  Here?
    <?
    confirm_form
    ($name$MailFromAddress);
    endif;
            }
    ?>
                          <------------------ or Here?
    <?
    function confirm_form($name$MailFromAddress) {
    ?> 
    .
    ..

  5. #5
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First initialize $submit to some $_POST['nameOfSubmit'] stuff, or are you allready ?
    +
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  6. #6
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by toggg
    Where is your $submit variable coming from ?

    - ? Dont really get u... but it should be comin from the fields in the form... I didn't display all the code coz its quite lengthy.... I tested out the code and believe the problem is in the order of code (as gavwvin mentioned) but as til now, cudn't diffuse the problm...

  7. #7
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by toggg
    First initialize $submit to some $_POST['nameOfSubmit'] stuff, or are you allready ?
    +

    Yup.. the submit has already been initialized... at line 5.. name of button is "submit"

    PHP Code:
    [1]<form name="form1" method="post" action="test_contct.php">
    [
    2].                                
    [
    3]...
    [
    4]...
    [
    5]<td height="26" colspan="2"><input name="submit" type="submit" id="submit2"   value="Submit"><input type="reset" name="Submit2" value="Reset">
    </
    td>
    </
    tr>
    </
    table

  8. #8
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK then:
    PHP Code:
        check_form($name, $MailFromAddress);
    endif;
    ?>
    <?  
    if(isset($submit)):
    You don't return no result from your check_form() function, it should return true or false
    and you should say
    PHP Code:
        $chk = check_form($name, $MailFromAddress);
    endif;
    ?>
    <?  
    if($submit && $chk):
    +
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  9. #9
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope.. no difference..

  10. #10
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    1) Even if your submit input has name submit, it will be overtook to variable $submit by PHP only if you have register global on, what's no more the default nowadays. Better do:
    if (isset($_POST['submit'])) {
    $submit = $_POST['submit'];
    }
    BTW is it submit or submit2, reset has same name ?
    2) Could you show how you get the check back from your function ?
    3) I would encourage you to clean a little bit your code so it is more easy to read.
    You don't need to open and close PHP tags just after another.
    Don't use this if (): ... else:... endif;
    Use better {} everywhere with beautiful indentation.
    4) Take courage
    +
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  11. #11
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    <?
    //initialise post
    $submit=$_POST['submit'];

    /* Declare some functions */

        
    function print_form($name$MailFromAddress) {
    ?>

    <form name="form1" method="post" action="test_contact.php">
    .                                
    ...
    ...
    <td height="26" colspan="2"><input name="submit" type="submit" id="submit2" value="Submit"><input type="reset" name="Submit2" value="Reset">
    </td>
    </tr>
    </table>


    <? }
    function 
    check_form($name$MailFromAddress) {

    if(!
    $name || !$MailFromAddress ) {
        print(
    "<font type=verdana size=1 color=red>You are missing some required fields:<br></font><br>");

        if(!
    $name) {
            print(
    "<font type=verdana size=1 color=red>- You need to fill in your <b>Name</b>.<br></font>"); }

        if(!
    $MailFromAddress) {
            print(
    "<font type=verdana size=1 color=red>- You need to fill in your <b>Email</b>.<br></font>"); }
        
        
    print_form($name$MailFromAddress);

    } else {

        
    $MailToAddress "me@myemailcom";
        
    $MailSubject "Enquiries Page - $select";
        if (!
    $MailFromAddress) {
            
    $MailFromAddress "$MailFromAddress";
        }
        
    $ip =  $HTTP_SERVER_VARS['REMOTE_ADDR'] ;
        
    $date date("d/m/Y H:i:s");
        
    reset($HTTP_POST_VARS);
        
    $Message .= "Sender Name : $name\n\n";
        
    $Message .= "Sender Email: $MailFromAddress \n\n";
        
    $Message .= "Sender Contact No.: $contact \n\n";
        
    $Message .= "Sender IP address : $ip \n\n";
        
    $Message .= "Sender Date and Time : $date \n\n";
        
    $Message .= "Feedback : \n";
        
    $Message .= "$feedback\n";
        
    mail("$MailToAddress""$MailSubject""$Message","From: $MailFromAddress);
        
    confirm_form($name$MailFromAddress);
    }
    }
    ?>

    <?
    function confirm_form($name$MailFromAddress) {
    ?>

    <font type=verdana size=1><b><br><span class="dateBold">
    Have a pleasant day!
    </span><span class="style2"><br></span></b> </font>  

    <span class="nameBold"><font type=verdana size=1><b>We thank you for your interest. </b></font></span><span class="style2"><font type=verdana size=1><b>.</b></font></span>
                                                
            
            
    <?    }    
    /* Begin Main Program */

    if(!$submit) {
        
    print_form("","","","","","");
    } else {
        
    check_form($name$MailFromAddress);
    }
    ?>

    </form>
    toggg is right you shouldn't use $submit if the variable has come from a form, you should use $_POST['submit']. Using $submit on its own (maybe not in this case, but in general) is a security issue- people can easily fake these variables... have a read of this article.

  12. #12
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=gavwvin]Try this:

    PHP Code:
    .
    ..
    ...
        if(!
    $MailFromAddress) {
            print(
    "<font type=verdana size=1 color=red>- You need to fill in your <b>Email</b>.<br></font>"); }
        
        
    print_form($name$MailFromAddress);

    } else {

        
    $MailToAddress "me@myemailcom";
        
    $MailSubject "Enquiries Page - $select";
        if (!
    $MailFromAddress) {
            
    $MailFromAddress "$MailFromAddress";
        }
        
    $ip =  $HTTP_SERVER_VARS['REMOTE_ADDR'] ;
        
    $date date("d/m/Y H:i:s");
        
    reset($HTTP_POST_VARS);
        
    $Message .= "Sender Name : $name\n\n";
        
    $Message .= "Sender Email: $MailFromAddress \n\n";
    .
    .
    .

    </
    form
    QUOTE]


    Thanks gavwvin,


    I tested out the code but it generated an error.. (below)

    Warning: reset(): Passed variable is not an array or object in /home/public_html/tech/test_contct.php on line 328



    --- line 328 points out to

    PHP Code:
    .
    .
       
    $MailToAddress "me@myemailcom";
        
    $MailSubject "Enquiries Page - $select";
        if (!
    $MailFromAddress) {
            
    $MailFromAddress "$MailFromAddress";
        }
        
    $ip =  $HTTP_SERVER_VARS['REMOTE_ADDR'] ;
        
    $date date("d/m/Y H:i:s");
        
    reset($HTTP_POST_VARS);                          <-------------here
        $Message 
    .= "Sender Name : $name\n\n";
        
    $Message .= "Sender Email: $MailFromAddress \n\n";
        
    $Message .= "Sender Contact No.: $contact \n\n";
        
    $Message .= "Sender IP address : $ip \n\n";
    .


  13. #13
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry fellas... my code kinda messy alright - kinda new to php.. was into asp...
    appreciate the help.. also am still yahoo-ing n googling to get some solutions too..

  14. #14
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't really see the point of that line anyway- remove it


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •