...

View Full Version : problem with IF and checkbox



simon.hunter
11-02-2010, 10:53 PM
Hi all
I have a very simple question, but it's been beating me up all day long!

On my form I have a checkbox:
"Send this request to both men and women players.
(If left unchecked, your request will be sent to players of your gender only.)"

Two possible outcomes:
Checkbox (when ticked) - I want the email request to be sent to all users within the database

Checkbox (when UNticked) - I want the form to query the gender of the user, and send the email request to all those users with matched gender.

I've started with an If statement, but can't get it to complete:


echo ' <p>Send this request to both men and women players.<font color="red">*</font></p>
<p<input type="checkbox" name="gender" value="on">
</p><p></p>' ;
if (isset($_SESSION['gender'])) {



I'm SURE it should be simple, but boy it's killing me!

Can anyone help me please?

Keleth
11-02-2010, 10:56 PM
It is remarkably simple, do you have any code yet?

Basically what you want to do is check if its ticked or not. Start a query, and if the box is ticked, concatenate a where statement about the gender. If you need to pull the gender from the table, if its ticked do one query, if its not, a different one. Then just pull and combine the data as you want (should be the same columns, since you're pulling from the same table), and send your email.

simon.hunter
11-02-2010, 11:11 PM
Hi Keleth
yes, I have a lot of code so far, the form and the email preview are contained within the following code.


<?php
// standard stuff first
require_once ('includes/config.inc.php');
$page_title = 'Find a Match';
echo session_id();
include ('includes/header.html');
// SH Start my stuff here
if (isset($_POST['submitted'])) {
require_once (MYSQL);
if ($e && $p)
{ // If email and password are ok, then everything's OK! If so...
// Query the database:
$q = "SELECT user_id, first_name, last_name, email, game_preference, skill_level, home_phone, work_phone, mobile_phone, gender, comments, user_level FROM users WHERE (email='$e' AND pass=SHA1('$p')) AND active IS NULL";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
// carry results of the query forward
if (@mysqli_num_rows($r) == 1) { // If A match was made in the database, log the user in and redirect them.
// Register the values & redirect:
$_SESSION = mysqli_fetch_array ($r, MYSQLI_ASSOC);
mysqli_free_result($r);
mysqli_close($dbc);
//$url = BASE_URL . 'index.php'; // Define the URL:
//ob_end_clean(); // Delete the buffer.
//header("Location: $url");
//exit(); // Quit the script.
}
//else { // No match was made.
//}
}
else /*{ // If everything wasn't OK.
echo '<p class="error">Please try again.</p>';
} */
mysqli_close($dbc);
} // End of SUBMIT conditional.
?>
<!--- START OF 'Find a Match' FORM-->
<HTML>
<HEAD></HEAD>
<BODY>
<a name="C5"></a><p class="second"> &nbsp;Looking for a last minute sub?&nbsp;&nbsp;Want to meet other players in the club?</p><p></p>
<FORM method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset STYLE =" width:60%; background-color:#C6DECD">
<legend><b>> &nbsp; Find a Match &nbsp; <</b></legend>
<p><h6> Step 1:</h6></p>
<p class="second">Make any temporary ammendments you wish for this search for a match. <br>Your permanent profile will not be altered.</p> <p></p>
<table class="table">
<tr>
<td class="tdright">Home Phone:</td>
<td class="tdleft"><input type="text" name="home_phone" value="<?php echo $_SESSION['home_phone']; ?>" /></td>
</tr>
<tr>
<td class="tdright">Work Phone:</td>
<td class="tdleft"><input type="text" name="work_phone" value="<?php echo $_SESSION['work_phone']; ?>" /></td>
</tr>
<tr>
<td class="tdright">Mobile Phone:</td>
<td class="tdleft"><input type="text" name="mobile_phone" value="<?php echo $_SESSION['mobile_phone']; ?>" /></td>
</tr>
<input type="hidden" name="email" value="<?php echo $_SESSION['email']; ?>">
<tr>
<td class="tdright">Seeking Game:</td>
<td class="tdleft"><select name="game_preference" size="1">
<option value='Single and Doubles'>Singles and Doubles</option>
<option value='Singles Only'>Singles Only</option>
<option value='Doubles Only'>Doubles Only</option></select></td>
</tr>
</table> <p></p>
<p><h6> Step 2:</h6></p>
<p class="second">Answer the following questions (Check all that apply):<br><font color="red">(* Required Fields)</font> </p>
<p></p>
<p class="second">Send this request to both men and women players.<font color="red">*</font></p>
<p class="second"<input type="checkbox" name="gender" value="">
&nbsp;&nbsp;&nbsp;&nbsp;(If left unchecked, your request will be sent to players of your gender only.)</p><p></p>
<p class="second">Match me with players of skill level <font color="red">*</font></p>
<p class="second">
<!--uses Array [] and then will loop through in the email preview-->
<input type="checkbox" name="chkskill_level[]" value="Advanced">Advanced
<input type="checkbox" name="chkskill_level[]" value="Intermediate">Intermediate
<input type="checkbox" name="chkskill_level[]" value="Beginner">Beginner</p> <p></p>
<p class="second">Preferred Contact method (tick one or many) <font color="red">*</font></p>
<p class="second">
<input type="checkbox" name="chkcontact[]" value="Home phone">Home phone
<input type="checkbox" name="chkcontact[]" value="Work phone">Work phone
<input type="checkbox" name="chkcontact[]" value="Mobile phone">Mobile
<input type="checkbox" name="chkcontact[]" value="email">email</p> <p></p>
<p><h6> Step 3:</h6></p>
<p class="second">Indicate your preferred playing times.<font color="red">*</font></p>
<p class="second">For example:</p>
<p class="second">&nbsp; &nbsp;I can play on Tuesdays at 09:30am.</p>
<p class="second">&nbsp; &nbsp;I need a 4th for doubles on Saturday May 27th at 17:30pm. </p>
<p class="second">&nbsp; &nbsp;Any time on the weekend will be OK. </p>
<p class="second"><textarea name="playing_times" rows="5" cols="60" wrap="off"></textarea> </p><br />
<p class="second"><input type="submit" name="submit" value="Preview the email that will be sent" ></p>
<p class="second"><b><a href="#C4">Click here</a> to preview the email that will be sent: </b><br>
</p> <p></p>
<table>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table> <p></p>
</fieldset> <p></p>
</FORM>
<p></p><br>
</BODY>
</HTML>
<!--- END OF 'Find a Match' FORM-->
<!--- START OF 'email preview' FORM-->
<?php
echo'';
$num = Rand(1,6);
//Based on the random number, gives a quote
switch ($num) {
case 1: echo "Time to Play at Windsor"; break;
case 2: echo "Someone at Windsor wants to play!"; break;
case 3: echo "Game Time at Windsor!"; break;
case 4: echo "Game, Set & Match at Windsor!"; break;
case 5: echo "Windsor Match Maker at work again!"; break;
case 6: echo "Looking for a game at Windsor?";
}
echo ' <a name="C4"></a>
<form action = "matchfind_email.php" method="post">
<fieldset STYLE=" width: 65% color:#00f; background-color:#F1F1F1 ">
<legend>> &nbsp; <b>email preview</b> &nbsp; <</legend>';
echo '<p></p><p class="second">Dear Windsor Tennis Player:</p>
<p class="second">This email Match request was sent to you by the Windsor Tennis MatchMaker program.</p>
<p class="second"> &nbsp; &nbsp;&nbsp; &nbsp; **** DO NOT REPLY TO THIS EMAIL **** </p>
<p class="second"> A match request that meets your requirements for play has been
entered via the Windsor Match Maker Program.</p><p></p>';
// Calls from the existing session, so appears as completed to the user. However, it's editable so the user can make
// on the fly changes to eg his home phone to send in the email

echo "<p class='third'><b>First Name:</b> {$_SESSION['first_name']}</p>" ;
echo "<p class='third'><b>Last Name:</b> {$_SESSION['last_name']}</p>" ;
echo "<p class='third'><b>Gender:</b> {$_SESSION['gender']}</p>" ;
echo "<p class='third'><b>Skill Level:</b> {$_SESSION['skill_level']}</p>" ;
echo "<p class='third'><b>Game Preference:</b> {$_SESSION['game_preference']}</p>" ;
// Playing_times: lets the user add in free text for using in this particular email ONLY. Changes NOT saved to the database
echo "<p> </p>";
// Trying to list only the ones that were selected
//Looking to list contact types identified by the check boxes
if(isset($_REQUEST['chkcontact'])){
$chkcontact=$_REQUEST['chkcontact'];
foreach ($chkcontact as $contact)
{
echo "<p class=\"third\"><i>Preferred contact method:<b> $contact</b></i></p><p></p>";
}
}
//defining your $_REQUEST variables first thing in your code, stating that they have the value posted
//by the user if the form has been submitted and assigning them an empty string otherwise
$home_phone = (isset($_REQUEST['home_phone']) ? $_REQUEST['home_phone'] : '');
$work_phone = (isset($_REQUEST['work_phone']) ? $_REQUEST['work_phone'] : '');
$mobile_phone = (isset($_REQUEST['mobile_phone']) ? $_REQUEST['mobile_phone'] : '');
$contact = (isset($_REQUEST['contact']) ? $_REQUEST['contact'] : '');
$playing_times = (isset($_REQUEST['playing_times']) ? $_REQUEST['playing_times'] : '');
$gender = (isset($_REQUEST['gender']) ? $_REQUEST['gender'] : '');
echo "<p class='third'><small>Contact player by</small> <b>Home Phone:</b>{$home_phone}</p>" ;
echo "<p class='third'><small>Contact player by </small><b>Work Phone:</b>{$work_phone}</p>" ;
echo "<p class='third'><small>Contact player by </small><b>Mobile:</b>{$mobile_phone}</p>" ;
echo "<p class='third'><small>Contact player by </small><b>email:</b>{$_SESSION['email']}</p>" ;
// pass variables eg email as hidden variable into the matchfind_email.php,
// This will generate the email that the recipients will see
echo"<input type='hidden' name='home_phone' value='{$home_phone}'/>";
echo"<input type='hidden' name='work_phone' value='{$work_phone}'/>";
echo"<input type='hidden' name='mobile_phone' value='{$mobile_phone}'/>";
echo"<input type='hidden' name='playing_times' value='$playing_times'/>";
echo"<input type='hidden' name='contact' value='{$contact}'/>";
echo"<input type='hidden' name='gender' value='{$gender}'/>";
//Looking to play with skill levels identified by the check boxes
//$chkskill_level=$_REQUEST['chkskill_level'];
// foreach ($chkskill_level as $skill_level)
// {
// echo "<p class=\"second\">I'm keen to play with players of $skill_level level</p>";
// }
echo '<p class="second"></p><p></p>';
echo '<p class="second">If you are interested, please contact this person directly to make arrangements for a match.</p>
<p class="second">&nbsp; &nbsp;&nbsp; &nbsp;**** DO NOT REPLY TO THIS EMAIL **** </p> <p></p>
<p class="second">For the latest information on tennis at Windsor visit our home page at
<a href="http://www.windsortennis.co.uk" title="Windsor Tennis Club website">www.windsortennis.co.uk</a><br>
Make sure you have booked a court as well - <a href="http://www.windsortennis.co.uk/courtbooker"
title="Windsor Court Booker"> www.windsortennis.co.uk/courtbooker</a>
</p><br/>
<p class="second">Thank you for using the Windsor Match Maker!</p><br>';
echo ' </table>
</fieldset>
';
echo '<p class="second"><b>If you are satisfied with your match request, press the button below to send it by email to
all matching players.</b></p>';
echo' <p class="second"><b>Otherwise, make your required </b><a href="#C5">changes on the top form.</a></p>';
echo '&nbsp; &nbsp;<input type="submit" name="submit" value="Send email match request" > <p></p>
</form>
';
?>
<!--- END OF 'email preview' FORM-->
<div id="content">
<?php
include ('includes/menu.html');
include ('includes/footer.html');
?>


I'm afraid I'm new to PHP and I learn best by looking at what others do and then ammending myself. You describe what to do very lucidly, but I'm afraid I don't even know how to check if a box has been ticked? :(

Happy to persevere if others are willing tho!

regards

Simon

Keleth
11-02-2010, 11:26 PM
Ok, well, that page does nothing to actually figure out the email, the users, anything.

Please, please please do not take this the wrong way, but you might be biting off more then you can chew. Without some basic fundamentals, you will end up making some pretty severe mistakes, end up on forums like this all the time with basic questions, and will drive yourself crazy. If you are not on a tight time limit for w/e you're working on, I highly recommend you take a step back and just work on some basics. What you're asking for here is not hard.

So first things first, describe your level of knowledge? Can you work with forms in PHP? Extracting data from a submitted form? The HTML alone needs work, as data is being outputted after the closing html tag, and thats an absolute NO. It has nested tables, the whole thing is a mess, which, even if you get working right now, will drive you insane. So lets gauge your knowledge level so we can figure out what you need to do.

simon.hunter
11-02-2010, 11:55 PM
Hi Keleth
thanks for your feedback. I'm thinking I should now have explained a little more about the project. It's a database which will match players of similar skills and abilities and help them find players of similar likes within my tennis club.
Ref the page of code: it's actually two parts/forms in one - <!--- START OF 'Find a Match' FORM--> is where the user fills in the form and <!--- START OF 'email preview' FORM--> is where the user gets to preview the contents of the email that he will be sending to users. I wanted to have them on the same page as it's easy for the user to understand. I'm able to have them on the sme page by using <?php echo $_SERVER['PHP_SELF']; ?>. I'm presuming the "data is being outputted after the closing html tag" you refer to is the code within <!--- START OF 'email preview' FORM-->?
The key thing here is that the forms work , with an email being sent to end users, and is error free.
So, my level of knowledge is certainly beyond beginner, yet probably not intermediate. I'm already working with forms in PHP and they are working with me. I'm extracting data from the form and sending it on to users by using the following matchfind_email.php.


<?php
require_once ('includes/config.inc.php');
$page_title = 'Find Match email';
include ('includes/header.html');
// Minimal form validation:
if (isset($_POST['submitted'])) {
require_once (MYSQL); }
echo '<div id="content">';

// create the body
$to=$_SESSION['email'];
$subject = 'Windsor Match Maker - someone wants you to play!';
$message = "Hi Windsor Member,
This email was sent to you by the Windsor Match Maker program.

**** DO NOT REPLY TO THIS EMAIL ****
*
A match request that meets your requirements for play has been
entered via the Windsor Match Maker Program.

First Name: {$_SESSION['first_name']}
Last Name: {$_SESSION['last_name']}
Gender: {$_SESSION['gender']}
Skill Level: {$_SESSION['skill_level']}
Game Preference: {$_SESSION['game_preference']}
Playing Times: {$_REQUEST['playing_times']}

Preferred contact method: {$_REQUEST['contact']}

Contact player by Home Phone: {$_REQUEST['home_phone']}
Contact player by Work Phone: {$_REQUEST['work_phone']}
Contact player by Mobile: {$_REQUEST['mobile_phone']}
Contact player by email: {$_SESSION['email']}

If you are interested, please contact this person directly to make arrangements for a match.

**** DO NOT REPLY TO THIS EMAIL ****

For the latest information on tennis at Windsor visit our home page at www.windsortennis.co.uk.
Make sure you have booked a court as well - www.windsortennis.co.uk/courtbooker!

Thank you for using the Windsor Match Maker!

";
$headers = 'From: tonto@f2s.com' . "\r\n" .
'Reply-To: noreply@huntersbelfast.co.uk' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send
mail($to, $subject, $message, $headers);

// confirmation messages
echo "<p>{$_SESSION['first_name']},</p>
<p>Your email has been sent to all players who match your requirements.</p>";

include ('includes/menu.html');
include ('includes/footer.html');


?>

Hope I haven't bored you!

Keleth
11-03-2010, 12:16 AM
Its not a bore, that's not my concern.

First and foremost, I'll gladly admit, I might have missed something, but you have data after closing the HTML tag. It doesn't matter what its in, its wrong. That can easily be the cause of problems, will cause a lot of browsers to show the page incorrectly, and can cause confusion as to what the problem actually is. It might not be a PHP error, but it is an error. Kind of like if you put a piece of tape to hold together a wire and it still not work... the tape could be the very problem!

The logic of what you're doing is spot on, how you're doing it isn't. Now, like I said, take no offense at that... when you're starting, you'll make mistakes, we all did.

So whats the key issue, not knowing how to extract checkbox data from a form? Simple enough. First, stop using _REQUEST. Promise me you'll never use _REQUEST AGAIN. _REQUEST draws from both GET and POST, and if you don't know which one you're expecting, no one can. If you're expecting GET, use _GET, if you're expecting POST, use _POST. You're using POST, which is appropriate for this form, good on ya.

To check if a checkbox is set:


$isCheckBoxSet = isset($_POST['checkbox_name'])?TRUE:FALSE;

Easy and straight forward.

simon.hunter
11-03-2010, 12:49 AM
Thanks Keleth
First of all, I'll have a go at revising the code so that the HTML closing tag is at the bottom of the page.
Secondly, once the firstly works (ha!) I will substitute POST for REQUEST (it's just that REQUEST is easier for me to understand). Am I to subsititue EVERY instance of REQUEST with POST?

and then I'll work on the checkbox again . ..

Thanks for your help!

Keleth
11-03-2010, 01:52 AM
You should NEVER use REQUEST. REQUEST access both POST and GET, so lets say you want a POST by the name of 'password'? I could put ?password=1 at the top of the page, and mess with your system. Again, YOU should be in control of every facet of your code. There should never be an ambiguous point, as ambiguity leads to security holes.

There's nothing about REQUEST that's easier, just lazier. I'm not saying you're being lazy, I used REQUEST until I realized how bad it was, but please don't use REQUEST. In this case, as your form is a POST form, yes, every REQUEST becomes POST. Of course, that's not global, because if you're working with a GET, the REQUEST should be a GET.

simon.hunter
11-03-2010, 01:26 PM
Lesson learnt!
Changes now made, moving on to trying the check checkbox is checked routine . .

I think that your code is
$isCheckBoxSet = isset($_POST['checkbox_name'])?TRUE:FALSE; one shortcut too far for me I'm afraid - am I right in thinking that my version matches your version?
if(isset($_POST['own_gender']) &&
$_POST['own_gender'] == 'Yes')
{
$own_gender = true;
}
else
{
$own_gender = false;
}
var_dump( $own_gender );

Can't get either of them to work :(

Keleth
11-03-2010, 03:53 PM
Yup, well, the issue is, some browsers don't pass on the value of a checkbox... they just pass on 'ON' if its checked (or a similar value), and don't pass the value if its not. So all you can do is check if its set. You are correct in your code's structure, just drop the check vs 'Yes' in the if statement.

The code I provided is a "shortcut" if. Basically what you wrote could be redefined as


$own_gender = isset($_POST['ownGender']) && $_POST['ownGender'] == 'Yes'?TRUE:FALSE;

(small note, when you're showing us PHP code, please use the PHP tags, not the code ones... the code you're working with here is simple, but it helps as you ask more complex questions).

simon.hunter
11-03-2010, 09:35 PM
Hi Keleth
still working on this!
If I'm reading it right, I think the following is getting me there:
First, the hidden statement

<input type="hidden" name="own_gender" value="<?php echo $_SESSION['gender']; ?>">

Then, the validation code:

if (!empty($_POST['own_gender'])) {
echo '<p>Ticked</p>';
} else {
echo '<p>not ticked</p>';
}

at this point I'm just out putting a statement that if checkbox is ticked, then 'ticked'; if not ticked, then 'Not ticked'.
And that all works up to there.

Now I have to do something along the lines of:
confirm my gender (by calling user_id)
scenario 1: if checkbox ticked, send to all
scenario 2: if checkbox unticked, then send to users own gender

This is where I run into problems: I can run a query to show me all users who are not Male


SELECT `user_id` , `gender`
FROM `users`
WHERE `gender` <> 'Male'
LIMIT 0 , 30


but I don't know how to run a query which will select users who are of the same gender as the user?

Help??

Keleth
11-03-2010, 10:47 PM
I couldn't answer that without knowing how your db is setup. Sounds like, thought I couldn't be sure.


SELECT users.* FROM users, users AS curUser WHERE curUser.user_id = whatever AND users.gender = curUser.gender

simon.hunter
11-04-2010, 01:02 AM
Thanks for the tip
Based on your code I've run the following using the phpMyAdmin interface:


SELECT users.* FROM users, users AS curUser WHERE curUser.user_id = `user_id`AND users.gender = curUser.gender

and get the following result:

#1052 - Column 'user_id' in where clause is ambiguous

I can see that using the AS temporarily assigns a table column a new name, so I did try renaming a few fields, but no joy.

For my own knowledge, what does the '.' in 'users.* FROM users' accomplish?

Keleth
11-04-2010, 05:26 AM
Yah, you're problem....


curUser.user_id = user_id

Which user_id are you refering to? So of course it won't work, it doesn't know if you mean users.user_id or curUsers.user_id. And in this case, its neither. Its the user_id of the current working user, which you feed in through PHP.

As for what the '.' means, that's how you define a table/column. So 'users.*' means all columns in the users table. 'users.user_id' means the user_id column in users.

simon.hunter
11-04-2010, 10:14 PM
Hmm, think I need to take a step back here, as I don't know how to feed in the user_id of the current user and I've spent most of the waking hours between last night and tonight googling for a resolution. And I can't actually make sense of the code that has been supplied: I think it's saying:
(your code)

SELECT users.* FROM users, users AS curUser WHERE curUser.user_id = whatever AND users.gender = curUser.gender

SELECT all columns from my 'users' table;
create on the fly a new USER
WHERE the user_id of the current user MATCHES 'user_id' (in my table)
and the users gender MAtches that of the 'new' user

I think I have got myself hung up on the last part, but, it's driving me nuts!

You stated earlier that you didn't know my database setup - I 've listed it as below, if that is of any help:


user_id
first_name
last_name
email
game_preference
playtime
skill_level
home_phone
work_phone
mobile_phone
gender
pass
user_level
comments
active
registration_date

Keleth
11-04-2010, 11:25 PM
Well, its not a "new" user, its the current user, but more or less, you have a grasp of it. Its referencing the user table twice, once for the current user (thus, where curUser.user_id = whatever... this would be just as if you were selecting the details for the current user, so curUser.userID = $user_id, or however its stored on PHP), and selecting all users in the user table (the second reference) that have the same gender as the curUser selected by the other where component.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum