SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Form help please

    Hello,
    I ahve built a comment form with some peoples help on this forum. Someone then suggested that guests may not want there email address to be visible on the site so i decided to add a checkbox to ask them. If ticked the address could be displayed. If unchecked then it couldn't. I want the box to be checked by default. I have tried it but something is always wrong and I cannot get that part to work. Below is the code i am using. Can anyone try to figure out where I am going wrong?

    PHP Code:
    <?php
    $form_name 
    null;
    $form_email null;
    $form_comment null;
    $form_rating null;
    $form_emailshow null;
    $error="";
    ?>

    <?php
    if (isset($_POST['submit'])) { //If form HAS been submitted

    if (!empty($_POST['name'])) {
    $form_name $_POST['name'];
    $form_name htmlentities($form_name);
    $form_name mysql_real_escape_string($form_name);
    }else{
    $form_name null;
    $error.="Enter your name<br />";
    }

    if (!empty(
    $_POST['email'])) {
    $form_email $_POST['email'];
    $form_email htmlentities($form_email);
    $form_email mysql_real_escape_string($form_email);
    }else{
    $form_email null;
    $error.="Enter your email<br />";
    }

    if (!empty(
    $_POST['rating'])) {
    $form_rating $_POST['rating'];
    $form_rating htmlentities($form_rating);
    $form_rating mysql_real_escape_string($form_rating);
    }else{
    $form_rating null;
    $error.="Choose your rating<br />";
    }

    if (!empty(
    $_POST['comment'])) {
    $form_comment $_POST['comment'];
    $form_comment htmlentities($form_comment);
    $form_comment mysql_real_escape_string($form_comment);
    }else{
    $form_comments null;
    $error.="Enter your comment<br />";
    }

    if (
    $_POST['emailshow'] = 1) {
    $form_emailshow $_POST['emailshow'];
    $form_emailshow htmlentities($form_emailshow);
    $form_emailshow mysql_real_escape_string($form_emailshow);
    $form_emailshow 1;
    } else {
    $form_emailshow 0;
    }

    $form_auth 0;
    $form_auth htmlentities($form_auth);
    $form_auth mysql_real_escape_string($form_auth);

    $form_rideid $rideid;
    $form_rideid htmlentities($form_rideid);
    $form_rideid mysql_real_escape_string($form_rideid);

    if (!empty(
    $error)) {
    echo 
    '<p style="color:#ff0000; class="center">' $error '</p>';
    require(
    'comment_form.inc.php');
    } else {



    //Add comment into database and send e-mail
    $addcomment mysql_query("INSERT INTO atm_ridecomments (comment_name, comment_email, comment_rating, comment_comment, comment_auth, comment_rideid, comment_emailshow) VALUES('$form_name', '$form_email', '$form_rating', '$form_comment', '$form_auth', '$form_rideid', '$form_emailshow')") or die(mysql_error());

    if (
    $addcomment) {
    echo 
    '<p>Comment added successfully.</p>';
    }else{
    echo 
    '<p>Comment failed.</p>';
    }



    $subject 'Thanks for posting a comment.';

    $message 'Hello,/nThanks for submitting a comment on Alton Towers Mania - www.atmania.com /n/nYour comment will be authorised and then posted up on the site./n/nThanks Again/nAlton Towers Mania';

    if (@
    mail($form_email$subject$message)) {
    echo(
    '<p>Confirmation E-Mail sent.</p>');
    } else {
    echo(
    '<p>Confirmation E-Mail failed to send.</p>');
    }
    }





    }else{ 
    //If form HAS NOT been submitted then show form
    ?>

    <?php
    require('comment_form.inc.php');
    ?>

    <?php
    }
    ?>

    The form itself which is included in the above code at certain points:
    PHP Code:
    <div>
        <form action="<?php $_SERVER['PHP_SELF']; ?>" name="addcomment" method="post">
        <div class="input_div"><label for="name">Name:</label><input class="form" id="name" type="text" name="name" value="<?php echo $form_name?>" /></div><br />
        <div class="input_div"><label for="email">E-Mail Address:</label><input class="form" id="email" type="text" name="email" value="<?php echo $form_email?>" /></div>
        <br />
        <div class="input_div"><label for="emailshow">Show E-Mail on site?:</label><input <?php if ($form_emailshow == '0') { echo '';}else{echo 'checked="checked"';} ?> class="form" id="emailshow" type="checkbox" name="emailshow" /></div>
        <br />
        <div class="input_div"><label for="rating">Rating:</label>
            <table style="margin-left:105px;" width="60%" border="0">
            <tr>
            <td>
            <input <?php if ($form_rating == '1star') { echo 'checked="checked"';}else{echo '';} ?> class="form" type="radio" name="rating" VALUE="1star" name="1star"></td><td><img src="img/votestars/1star.gif" alt="1 Star Rating" />
            </td></tr><tr><td>
            <input <?php if ($form_rating == '2star') { echo 'checked="checked"';}else{echo '';} ?> class="form" type="radio" name="rating" VALUE="2star" name="2star"></td><td><img src="img/votestars/2star.gif" alt="2 Star Rating" />
            </td></tr><tr><td>
            <input <?php if ($form_rating == '3star') { echo 'checked="checked"';}else{echo '';} ?> class="form" type="radio" name="rating" VALUE="3star" name="3star"></td><td><img src="img/votestars/3star.gif" alt="3 Star Rating" />
            </td></tr><tr><td>
            <input <?php if ($form_rating == '4star') { echo 'checked="checked"';}else{echo '';} ?> class="form" type="radio" name="rating" VALUE="4star" name="4star"></td><td><img src="img/votestars/4star.gif" alt="4 Star Rating" />
            </td></tr><tr><td>
            <input <?php if ($form_rating == '5star') { echo 'checked="checked"';}else{echo '';} ?> class="form" type="radio" name="rating" VALUE="5star" name="5star"></td><td><img src="img/votestars/5star.gif" alt="5 Star Rating" />
            </td></tr></table>
        </div><br />

        <div class="input_div"><label for="comment">Comments:</label><textarea cols="22" rows="5" class="form" id="comment" name="comment"><?php echo $form_comment ?></textarea><br /><br />
        </div>
        <br /><br />
        <div class="center">
        <input type="submit" value="Submit Comment" name="submit" />
        <input type="reset" value="Reset Form" name="reset" />
        </div>
        </form>
    </div>
    Thanks

    Neil

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Hi Neil,

    in your php code use:
    PHP Code:
    $checked = isset($_POST['emailshow']) ? ' checked' ''
    and on your form:
    PHP Code:
    <input <?php  echo $checked?> class="form" id="emailshow" type="checkbox" name="emailshow"  />
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found there a simple mistake here in your PHP code:
    PHP Code:
    if ($_POST['emailshow'] = 1) { 
    Make it just like
    PHP Code:
    if ($_POST['emailshow'] == 1) { 
    Or simply
    PHP Code:
    if ($_POST['emailshow']) { 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm getting this error:

    Notice: Undefined index: emailshow in C:\wamp\www\template atm\includes\comment.inc.php on line 49

    line 49 is this:

    if ($_POST['emailshow'] == 1) {

  5. #5
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Spikez, Just to ask you, Where do I put the code you suggested? At the top of the form page? or somewhere on the comment page?

  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just replace your this line:
    PHP Code:
    if ($_POST['emailshow'] = 1) { 
    To
    PHP Code:
    if ($_POST['emailshow']) { 
    It will work.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  7. #7
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, It does work but take this scenario:

    If I fill in the form and keep the box CHECKED and forget the fill in 1 of the other fields, the form reloads and i get the red text saying where a field has been missed out and the checkbox retains it's check just as it should.

    If the same scenario happens but the box is unchecked before submittingt he form, the form shows again with the box unchecked like it should but i get this error:

    Notice: Undefined index: emailshow in C:\wamp\www\template atm\includes\comment.inc.php on line 49

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can we remove Notice being shown with @ before variable name like this?
    PHP Code:
    <input <?php if (@$form_emailshow == 1) { echo "checked=\"checked\"";}else{ echo "";}?> class="form" id="emailshow" type="checkbox" name="emailshow" />
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Neil,
    replace
    PHP Code:
    if ($_POST['emailshow'] = 1) { 
    $form_emailshow $_POST['emailshow']; 
    $form_emailshow htmlentities($form_emailshow); 
    $form_emailshow mysql_real_escape_string($form_emailshow); 
    $form_emailshow 1
    } else { 
    $form_emailshow 0

    with
    PHP Code:
    $checked = isset($_POST['emailshow']) ? ' checked' ''
    that should sort you out
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  10. #10
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But wouldn't doing that upset the entering the 1 or 0 into the database field using the code in comment.php?

    Thanks

    Neil

  11. #11
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    ah gotcha
    PHP Code:
    if(isset($_POST['emailshow'])) {
        
    $checked ' checked';
        
    $form_emailshow 1;
    } else {
        
    $checked ' ';
        
    $form_emailshow 0
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  12. #12
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I do all that i get this error:

    Notice: Undefined variable: checked in C:\wamp\www\template atm\includes\comment_form.inc.php on line 6
    class="form" id="emailshow" type="checkbox" name="emailshow" />

    Line 6 is this:

    <div class="input_div"><label for="emailshow">Show E-Mail on site?/label><input <?php echo $checked; ?> class="form" id="emailshow" type="checkbox" name="emailshow" /></div>

  13. #13
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    $checked '';
    if(isset(
    $_POST['emailshow'])) { 
        
    $checked ' checked'
        
    $form_emailshow 1
    } else { 
        
    $checked ' '
        
    $form_emailshow 0;

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  14. #14
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I still get this showing:

    PHP Code:
    NoticeUndefined variablechecked in C:\wamp\www\template atm\includes\comment_form.inc.php on line 6
    class="form" id="emailshow" type="checkbox" name="emailshow" /> 

  15. #15
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cna anyone help wit this?

  16. #16
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So could you please paste your whole code once again that you might have changed it now?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  17. #17
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Morning Neil, must have been a bit tired when i posted the last part!
    it will still be undefined as it is inside the if(isset($_POST.... check.
    where you have your variables at the top, add the $checked to them
    PHP Code:
    <?php 
    $form_name 
    null
    $form_email null
    $form_comment null
    $form_rating null
    $form_emailshow null
    $error=""
    $checked "";
    ?>
    That should sort it
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  18. #18
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, That works fine but now, the box is not checked by default when the page is loaded. Do I just change the $checked variable to equal checked?

  19. #19
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heya,
    Sorted! - Yes that did work! I just changed $checked to equal checked!

    Cheers all!

    Thanks

    Neil

  20. #20
    SitePoint Enthusiast
    Join Date
    May 2007
    Location
    Cyberspace
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What was the final code?

  21. #21
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the final code for the form and the comment pages:

    Form:

    PHP Code:
    <div>
        <form action="<?php $_SERVER['PHP_SELF']; ?>" name="addcomment" method="post">
        <div class="input_div"><label for="name">Name:</label><input class="form" id="name" type="text" name="name" value="<?php echo $form_name?>" /></div><br />
        <div class="input_div"><label for="email">E-Mail Address:</label><input class="form" id="email" type="text" name="email" value="<?php echo $form_email?>" /></div>
        <br />
        <div class="input_div"><label for="emailshow">Show E-Mail on site?:</label><input <?php echo $checked?> class="form" id="emailshow" type="checkbox" name="emailshow" /></div>
        <br />
        <div class="input_div"><label for="rating">Rating:</label>
            <table style="margin-left:105px;" width="60%" border="0">
            <tr>
            <td>
            <input <?php if ($form_rating == '1star') { echo 'checked="checked"';}else{echo '';} ?> class="form" type="radio" name="rating" VALUE="1star" name="1star"></td><td><img src="img/votestars/1star.gif" alt="1 Star Rating" />
            </td></tr><tr><td>
            <input <?php if ($form_rating == '2star') { echo 'checked="checked"';}else{echo '';} ?> class="form" type="radio" name="rating" VALUE="2star" name="2star"></td><td><img src="img/votestars/2star.gif" alt="2 Star Rating" />
            </td></tr><tr><td>
            <input <?php if ($form_rating == '3star') { echo 'checked="checked"';}else{echo '';} ?> class="form" type="radio" name="rating" VALUE="3star" name="3star"></td><td><img src="img/votestars/3star.gif" alt="3 Star Rating" />
            </td></tr><tr><td>
            <input <?php if ($form_rating == '4star') { echo 'checked="checked"';}else{echo '';} ?> class="form" type="radio" name="rating" VALUE="4star" name="4star"></td><td><img src="img/votestars/4star.gif" alt="4 Star Rating" />
            </td></tr><tr><td>
            <input <?php if ($form_rating == '5star') { echo 'checked="checked"';}else{echo '';} ?> class="form" type="radio" name="rating" VALUE="5star" name="5star"></td><td><img src="img/votestars/5star.gif" alt="5 Star Rating" />
            </td></tr></table>
        </div><br />

        <div class="input_div"><label for="comment">Comments:</label><textarea cols="22" rows="5" class="form" id="comment" name="comment"><?php echo $form_comment ?></textarea><br /><br />
        </div>
        <br /><br />
        <div class="center">
        <input type="submit" value="Submit Comment" name="submit" />
        <input type="reset" value="Reset Form" name="reset" />
        </div>
        </form>
    </div>



    Comment:

    PHP Code:
    <?php $form_name null;
    $form_email null;
    $form_comment null;
    $form_rating null;
    $form_emailshow null;
    $error="";
    $checked "checked";
    ?>  

    <?php
    if (isset($_POST['submit'])) { //If form HAS been submitted

    if (!empty($_POST['name'])) {
    $form_name $_POST['name'];
    $form_name htmlentities($form_name);
    $form_name mysql_real_escape_string($form_name);
    }else{
    $form_name null;
    $error.="Enter your name<br />";
    }

    if (!empty(
    $_POST['email'])) {
    $form_email $_POST['email'];
    $form_email htmlentities($form_email);
    $form_email mysql_real_escape_string($form_email);
    }else{
    $form_email null;
    $error.="Enter your email<br />";
    }

    if (!empty(
    $_POST['rating'])) {
    $form_rating $_POST['rating'];
    $form_rating htmlentities($form_rating);
    $form_rating mysql_real_escape_string($form_rating);
    }else{
    $form_rating null;
    $error.="Choose your rating<br />";
    }

    if (!empty(
    $_POST['comment'])) {
    $form_comment $_POST['comment'];
    $form_comment htmlentities($form_comment);
    $form_comment mysql_real_escape_string($form_comment);
    }else{
    $form_comments null;
    $error.="Enter your comment<br />";
    }

    $checked '';
    if(isset(
    $_POST['emailshow'])) {
    $checked ' checked';
    $form_emailshow 1;
    } else {
    $checked ' ';
    $form_emailshow 0;
    }

    $form_auth 0;
    $form_auth htmlentities($form_auth);
    $form_auth mysql_real_escape_string($form_auth);

    $form_rideid $rideid;
    $form_rideid htmlentities($form_rideid);
    $form_rideid mysql_real_escape_string($form_rideid);

    if (!empty(
    $error)) {
    echo 
    '<p style="color:#ff0000; class="center">' $error '</p>';
    require(
    'comment_form.inc.php');
    } else {



    //Add comment into database and send e-mail
    $addcomment mysql_query("INSERT INTO atm_ridecomments (comment_name, comment_email, comment_rating, comment_comment, comment_auth, comment_rideid, comment_emailshow) VALUES('$form_name', '$form_email', '$form_rating', '$form_comment', '$form_auth', '$form_rideid', '$form_emailshow')") or die(mysql_error());

    if (
    $addcomment) {
    echo 
    '<p>Comment added successfully.</p>';
    }else{
    echo 
    '<p>Comment failed.</p>';
    }



    $subject 'Thanks for posting a comment.';

    $message 'Hello,/nThanks for submitting a comment on Alton Towers Mania - [url]www.atmania.com[/url] /n/nYour comment will be authorised and then posted up on the site./n/nThanks Again/nAlton Towers Mania';

    if (@
    mail($form_email$subject$message)) {
    echo(
    '<p>Confirmation E-Mail sent.</p>');
    } else {
    echo(
    '<p>Confirmation E-Mail failed to send.</p>');
    }
    }





    }else{ 
    //If form HAS NOT been submitted then show form
    ?>

    <?php
    require('comment_form.inc.php');
    ?>

    <?php
    }
    ?>


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
  •