I'm still new in PHP and I need to know how I can create two or more conditional statements for a single elements. For instance, maybe if I had a form that I wanted to perform a certain action if some conditions were true.
I understand that one of the way is to group the statements like:
if(arg1 && arg2 && arg3) {
echo "All of these statements are true";
}
How can I separate this condition but making sure that it executes at the same time?
You can use nested if statements to accomplish the same check.
if(arg1)
{
if(arg2)
{
if(arg3)
{
echo "All of these statements are true";
}
}
}
Try this.
if(($something == 1 ) && ($someotherthing !=7) && ($something == 2)) {
echo "All of these statements are true";
}
Changed answer after what I understood of you clarification:
If you want to know if the posted form has all the required fields in PHP then you could write :
if(isset($_POST['field_name']) && $_POST['field_name'] != "")
{
//The field is set and not empty.
}
else
{
echo "Field not set...";
}
w3schools tutorial on forms :
http://www.w3schools.com/php/php_forms.asp
As for the numeric part of your question take a look at regex :
http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial
EDIT 2 : As someone mentioned if you want to know if the field is set BEFORE being sent to the server, you will need to use Javascript.
Related
I have an html form that depending on the checkbox my java-script change it from GET to POST. This piece of code is currently working.
My question is I have a variable on the server side that I need to get. The html variable is sent either as POST or GET, but not sure how to retrieve that variable regardless or what method the html uses. I know how to get the variable as either POST or GET manually, but Not sure how to go about accomplishing this automatically. Any suggestions?
$myVariable = $_GET['htmlVariable'] or $myVariable = $_POST['htmlVariable']
1) Use $_GET if you know that data is coming via URL parameter
if (isset($_GET['htmlVariable'] && $_GET['htmlVariable'] != '') {
$htmlVariable = $_GET['htmlVariable'];
}
2) Use $_POST if you know that data is coming via HTTP POST method
if (isset($_POST['htmlVariable'] && $_POST['htmlVariable'] != '') {
$htmlVariable = $_GET['htmlVariable'];
}
3) If you don't know use $_REQUEST
if (isset($_REQUEST['htmlVariable'] && $_REQUEST['htmlVariable'] != '') {
$htmlVariable = $_GET['htmlVariable'];
}
It sounds like you're looking to check if one of the two methods is found. If it is, use that method, and if not, use the other method. To achieve this you can use isset(), and note that you'll also want to check that the string isn't empty:
if (isset($_GET['htmlVariable'] && $_GET['htmlVariable'] != '') {
$myVariable = $_GET['htmlVariable'];
}
else if (isset($_POST['htmlVariable'] && $_POST['htmlVariable'] != '') {
$myVariable = $_POST['htmlVariable'];
}
else {
$myVariable = 'something else';
}
Also note that the order in which you check for the methods may make a difference, depending on your logic.
THE PROBLEM
The problem I've having is I can't override the current page with include.
if ($LS::getUser('clan') || isset($_GET['clan']) && !isset($_GET['search'])) {
include("res/templates/clan-overview.php");
} else {
include("res/templates/clan-search.php");
}
I don't get why I can't change the page with clan-search.php because I said that if $_GET['search'] is NOT set execute the else statement. But for some weird reason I'm still getting true even though I said that if the user has a CLAN || $_GET['clan'] AND $_GET['search'].
What I've Tried
That code I put at the top is actually the modified version of my original code which I thought would fix the problem. If you are interested in looking at the original code which I don't think will help you here it is:
if ($LS::getUser('clan') || isset($_GET['clan'])) {
include("res/templates/clan-overview.php");
} else if (isset($_GET['search'])) {
include("res/templates/clan-search.php");
}
#Drew pierce gave you first response as you need to wrap the OR parts as a group. try this:
if (($LS::getUser('clan') || isset($_GET['clan'])) && !isset($_GET['search'])) {
I'm trying to implement multiple if(isset()) statements but I can't get it to work.
example:
if (isset($_GET['a']) or isset($_GET['b'])) {
// HTML
} else {
// HTML
<a href="?link=a>LinkA</a>
<a href="?link=b>LinkB</a>
}
When I click a or b I still got the else statement executed.
I also tried:
if (isset($_GET['a'] or $_GET['b']))
but then I get a error
I'm trying to display different pages on different $_GET requests.
Can someone point me in the right direction or is this not the right way to do this?
Change if (isset($_GET['a']) or isset($_GET['b'])) To:
if ( (isset($_GET['link']) && $_GET['link'] == 'a') OR (isset($_GET['link']) && $_GET['link'] == 'b']) )
You are checking wrong variable.
You need to check $_GET ['link']
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.
i have done something like this:
<form action="validate.php" method="get">
Id :<input type="text" name="pID"/><br/><br/>
Name :<input type="text" name="pName"/><br/><br/>
Description :<input type="text" name="pDesc"/><br/><br/>
Price :<input type="text" name="pPrice"/><br/><br/>
<input type="submit" name="pSub"/>
</form>
my validate.php contains :
<?php
if (!empty($_GET['pID']) || !empty($_GET['pName']) || !empty($_GET['pDesc']) || !empty($_GET['pPrice'])){
if(is_numeric($_GET['pID']) || is_numeric($_GET['pPrice']))
{
echo "</br>Your ID :".$_GET["pID"]."</br>";
echo "Name is :".$_GET["pName"]."</br>";
echo "Description :".$_GET["pDesc"]."</br>";
echo "and Price :".$_GET["pPrice"]."</br>";
}
else{echo "Pls See That ID and Price are Numerical";}
}else{
echo "Fill up All The Values";
}
?>
is not working properly ,
1st if conditions doesn't work properly
ie. if i left blank "Name" input field message should have come saying
"Fill up All The Values "...... instead its showing the list of inputs
is there any other way to validate form (PHP)
You are using the wrong operator: || means "logical OR"; what you seem to be looking for is &&, that is "logical AND".
The code does exactly what you told it to do (see the documentation); the fact that you intended something else is not relevant to the computer:
if (!empty($_GET['pID']) || !empty($_GET['pName']) || !empty($_GET['pDesc']) || !empty($_GET['pPrice']))
means "if pID is not empty OR pName is not empty OR ..."; as soon as one or more of the fields are not empty, the condition evaluates to true.
What you can do to get what you meant:
replace OR with AND ( && )
use if (!(empty($_GET['pID']) || empty($_GET['pID'] ...)) - note that the whole expression is negated in parentheses
(read on De Morgan's laws to see why these two solutions are equivalent)
It's probably better that you switch the conditions around, like this:
if(empty($_GET['pID']) || empty($_GET['pName']) || empty($_GET['pDesc']) || empty($_GET['pPrice'])) {
echo "Please fill up all the values";
} else {
// Do other validation.
}
This way, you know your inputs are correct before you do anything else. Obviously I've not tested this, but it should work as expected. What you were saying before was asking if ANY of the inputs was not empty, do the additional validation. As one of the other commenters explained, if you wanted to do that, you should be using && rather than ||.
Changing it around just makes it a bit clearer!
It's a logic problem with your code. Using || in this situation means that if ANY of those inputs contains a value, then the first condition is met. What you want to do is AND, not OR, so that the first condition is only met if all of the inputs are !empty.
I don't remember for sure what the AND operator is for PHP, since it's been a long time, but it's probably &&.
The question already has been answered, but there is one more thing,
I recommend that you use $_POST instead of $_GET because $_POST is way more secure, as you use HTML Forms. You could look it up on internet.
Here is a link, the first answer says it all: Difference between $_POST & $_GET
This is just wrong
if (!empty($_GET['pID']) || !empty($_GET['pName']) || !empty($_GET['pDesc']) || !empty($_GET['pPrice'])){}
You need to make it like this:
if (!empty($_GET['pID'], $_GET['pName'], $_GET['pDesc'], $_GET['pPrice'])){}
And no need to make ORs at all. Also, you'd better check if any of the given values are empty and throw error on that.