SitePoint Sponsor |
|
User Tag List
Results 1 to 21 of 21
Thread: Form help please
-
Jun 29, 2007, 04:27 #1
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>
Neil
-
Jun 29, 2007, 04:55 #2
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
Hi Neil,
in your php code use:
PHP Code:$checked = isset($_POST['emailshow']) ? ' checked' : '';
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.....
-
Jun 29, 2007, 04:56 #3
- 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) {
PHP Code:if ($_POST['emailshow'] == 1) {
PHP Code:if ($_POST['emailshow']) {
Mistakes are proof that you are trying.....
------------------------------------------------------------------------
PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5
-
Jun 29, 2007, 05:00 #4
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) {
-
Jun 29, 2007, 05:01 #5
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?
-
Jun 29, 2007, 05:03 #6
- 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) {
PHP Code:if ($_POST['emailshow']) {
Mistakes are proof that you are trying.....
------------------------------------------------------------------------
PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5
-
Jun 29, 2007, 05:06 #7
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
-
Jun 29, 2007, 05:14 #8
- 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
-
Jun 29, 2007, 05:18 #9
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- 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;
}
PHP Code:$checked = isset($_POST['emailshow']) ? ' checked' : '';
Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Jun 29, 2007, 05:25 #10
But wouldn't doing that upset the entering the 1 or 0 into the database field using the code in comment.php?
Thanks
Neil
-
Jun 29, 2007, 05:32 #11
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- 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.....
-
Jun 29, 2007, 05:49 #12
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>
-
Jun 29, 2007, 07:17 #13
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- 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.....
-
Jun 29, 2007, 07:31 #14
I still get this showing:
PHP Code: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" />
-
Jun 29, 2007, 14:26 #15
Cna anyone help wit this?
-
Jun 30, 2007, 00:52 #16
- 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
-
Jun 30, 2007, 03:08 #17
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- 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 = "";
?>Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Jun 30, 2007, 03:59 #18
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?
-
Jun 30, 2007, 04:01 #19
Heya,
Sorted! - Yes that did work! I just changed $checked to equal checked!
Cheers all!
Thanks
Neil
-
Jun 30, 2007, 04:05 #20
- Join Date
- May 2007
- Location
- Cyberspace
- Posts
- 64
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What was the final code?
-
Jun 30, 2007, 04:34 #21
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