Using if else statement based off of $_POST value to echo message - php

Pretty sure this is a quick and easy question but I have a form that on action POST goes to a confirmation page. I need a message to display on the confirmation page if the user selects county1 but if user selects county2, county3, or county4. However, when I setup the statement it's not working. Probably a syntax error or two on my part. Any help would be greatly appreciated.
A messy idea of what I think should work:
<?php $county=$_POST['County'];
if ($county="Polk") {
echo "Important message about your county"; }
else {
echo " "; // Or nothing at all
}
?>
But
<?php echo $_POST['County'] ?>
displays the name of the county so I know the submission is carrying through. Thoughts on why my above code wouldn't be working? If you could flag syntax errors or code placement that'd be much appreciated! Thank you!

Inside the if condition you should use two equal operators instead of one . try this code
<?php
$county = isset($_POST['County'])?$_POST['County']:"";
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
?>

Use double equal in your if statement for comparison
See the answer and read the comment to understand why you have to change it
if ($county="Polk") {
Single equal is assignment operator, it assign value
Change this line to this
if ($county=="Polk") {
So your whole code should look like this
$county=$_POST['County'];
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
Use double equal sign, double equal is comparison operator,
Here you are checking if the $county is equal to Polk or not,
Means you are comparing value

Related

PHP if/then statement

Need help with some php to modify a WP plugin which is paid memberships pro. Terrible at php here.
What am trying to do is create one line of code that would say if the membership level equals XXX then print this link. SO the variable I would need are somewhere in this line I imagine:
<li><strong><?php _e('Membership Level', 'paid-memberships-pro' );?>:
</strong> <?php echo $current_user->membership_level->name?></li>
The above is just a snippet of code already found in the page I want to create this if/then link statement.
so something like:
<?php if($Membership Level == $Conflicts of Interest #14124(that's the name
of one level) then print this link.
AM I making sense?
Edit:
Thanks to some help below, this seems to work:
<?php if($membership_level == 'Conflicts of Interest #14124') {
echo "<a href=\"conflicts-of-interest-in-modern-legal-practice-and-internal-
investigations-14124/\">Testing</a>";
}
?>
But the 'Conflicts of Interest #14124' doesn't match even though it is the correct name.
Then general If else statement in the html page
<?php if($membership_level == 'string to be compared') {
echo 'the link that you want to print if it is a string. you can replace this string
with a variable if the value is in variable'
} else {
'any other text if require else you can remove the else block'
}
?>
Hope that helps.

PHP validating query string if statement

This seems like it should be easy but I can't for the life of me get this to work, I'm checking a variable in a query string, if it returns a Yes it redirects to a new page. If it returns a no I want it to display an error message above the form.
My issue is with displaying the error message, it redirects when var=Yes but when it gives me var=no I can't get it to display the error message. If I replace $errormsg with header(..); it works so I know it's not my if statement.
if (isset($_GET['var'])) {
$var = $_GET['var'];
$errormsg = '';
if ($var == 'Yes') {
header( 'Location: http://www.google.com' ); exit;
}
else if ($var == 'no') {
$errormsg = 'This is an error message.';
}
};
And this is the error message above the form:
<div class="errormsg"><?php echo $errormsg; ?></div>
My PHP ability is limited and I can't figure out what I'm doing wrong, any help will be appreciated.
Thanks!
My guess is the $var is not returning what you are expecting (possibly a "No" instead of a "no" as mjayt suggested in the comments.
First step would be to debug, and determine what $var is actually returning. Try simple print statements and go from there.
Try to echo $errormsg directly under the if block without any HTML. See if your text is returned.
Think you might remove that semi colon from that last curly brace also

I have a check box on a form that should not be allowed to be checked if there isn't corresponding data inside sql

I created a variable to check and echo out results for a particular patient:
<?php
$ex = sql::value("select 1 email from " . event_table('email_rules') . " where patient_id={$patient->hex}");
if ($ex == 1) {echo "Records";} else {echo "None";}
?>
My html input is simple, cbox is checkbox:
<tr><td>Email</td><td><?php cbox('email'); ?></td></tr>
If someone checks the email box but there is no email in sql than i need to throw an alert saying there is no email associated.
I'm assuming I could use jquery or javascript to get this accomplished in a clean and concise manner. I hope I'm fairly clear on what i'm trying to accomplish. Help would be greatly appreciated. Thank you.

While statement repeats infinitely

I have a website where i need to use a while statement, but when i use it, it repeats the echo infinitely. Although it looks like i could make it work without while, that isnt so, this is a simplified version of a final product that will need while.
<?php
$passlevel = '0';
while ($passlevel == '0')
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$passlevel == '1';
}
else
{
echo "you didn't select purple.";
}
}
else echo "contact webmaster";
}
?>
Why is it echoing either contact webmaster or you didnt select purple an infinite number of times?
First, you probably need to change:
$passlevel == '1';
to
$passlevel = '1';
The first is a comparison equals, not an assignment equals.
Second, if $color is not #800080, then the loop does not terminate and thus repeats forever as nothing in the loop causes the value to change.
I'm not entirely sure of the point of this loop in the first place. It should work perfectly fine without the loop, however you've stated that your code is a simplified version of something more complicated that indeed needs a loop. Perhaps you can elaborate.
You're not providing any way out of the loop. If $_GET['box_1_color'] isn't purple the first time through the loop, it can't possibly become anything else the second time through the loop, so it'll keep being the wrong color each and every time.
I'm not certain what you intended for this loop to accomplish. If you're trying to have the user enter a new value each time, you won't be able to do that with a loop in PHP. You'll have to regenerate the entire page (with an error message, presumably) and ask the visitor to submit the form again.
In the case of "contact webmaster", you need to break out of the loop, either with the break expression or by setting your $passlevel to anything other than zero. A more serious real problem is revealed in #Mike Christensen's answer, though
If $_GET['box_1_color'] is not set, the variable $passlevel will never be changed.
<?php
$passlevel = 0;
while ($passlevel == 0 || $passlevel == 2)
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$passlevel = 1;
}
else
{
echo "you didn't select purple.".'try again.';
}
}
else
{
echo "contact webmaster";
$passlevel = 2;
}
}
?>
You need to define another passlevel for failure, to stop the while loop. Also, don't put any quotes around integers.

How to check if the user has entered or not entered data to Mysql using PHP?

I'm trying to see if the user has entered a website URL into the database with the following code. If the user did not enter their website's URL, do not display anything. If the user did enter their website, display the website.
I think I'm doing it wrong.
<?php
if (!empty($url))
{
echo'';
} else {
echo'p>Website: <?php echo "http://","$url"; ?></p>';
}
?>
Apart from the wrong order (you need to remove ! before the empty()), you also have some errors in the echo part: the < is missing for the < p > tag and you are including php tags in your string.
It should read something like:
echo '<p>Website: http://' . $url . '</p>';
what you've got there is that you're checking if it's NOT empty (that is, there is some data), you're echoing an empty string. If it IS empty, then you're echoing it out.
Remove the ! in the first line and you should be right.
You're simple mismatching the meaning of "!" :
<?php
if (!empty($url))
{
echo'p>Website: <?php echo "http://","$url"; ?></p>';
} else {
echo'';
}
?>

Categories