Empty value clause for a string not working - php

I am trying to get value with ajax and the values are in form of string, but the problem is when I'm trying to use a condition of if value empty then return this or else do that
My code is
if (empty($title) || empty($thumbnail) || empty($link))
{
echo "404";
}
else
{
some custom line
}
My problem is that a 404 is returned even if the value of any one of them (title, thumbnail, or link) is not empty. Can any one point me where I am wrong?
Here's how I am getting value from ajax:
$title = mysql_real_escape_string($_REQUEST['title']);
$thumbnail = mysql_real_escape_string($_REQUEST['thumbnail']);
$link = mysql_real_escape_string($_REQUEST['link']);

First of all I suggest you to do not use empty, cause it got some specific behave.
Second: I don't get why you are using mysql_real_escape_string, you have to query a database?
Moreover i guess you (as #havelock pointed out) wanted to get a 404 if none of that values are set up.
I'm for this:
if($title && $thumbnail && $link){
//all fine..
}
else 404

Related

how to get a post or get variable

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.

if($_POST['dropdown_value'] == null) is not also returning true if no <option> value was selected

I have Ajax posting to a php script. One of the posts is a . In the php script, I check if an option in the dropdown was selected. If not, I fill in a default value. This is then submitted to a database. See my below code for checking if an was selected:
if($_POST['dropdownValue'] == null){}
99.9% of the time, this works. I don't select an option, and the default value is returned and this is stored in my database. But now I'm finding NULL rows in my database that are filled with the value of $_POST['dropdownValue']. Should I be using the function is_null()? Or isset()? I saw another post that said to check it with $_POST['dropdownValue'] == ''. Would that be better?
you could try using the isset for this i think
if(isset($_POST['dropdownValue']) && ($_POST['dropdownValue'] != null)
{
'insert that data into my base'
}
Let's just use isset:
if( isset($_POST['fromPerson']) )
{
$fromPerson = '+from%3A'.$_POST['fromPerson'];
echo $fromPerson;
}

isset not givig desired result using && operator

if(isset($_POST["submit"])){
if (isset($_POST["name"]) && isset($_POST["roll"])){ // checking whether both text boxes are filled up
echo "$_POST[name] $_POST[roll]"; // just displays some info
}
else{ //not being executed when both text boxes are empty
header("Location:interface.php");
}
}
the above is a php code where im trying to check whether two text boxes are filled in or else i will navigate to another page. The problem is though if i fill up one text box the && condition doesn't work , it gets into the if part and echoes one value.
There is error (missing ') in:
echo "$_POST[name] $_POST[roll]";
It has to be $_POST['name'] and $_POST['roll']
UPDATE
isset() only is not enough. You should also check if it is not empty, like this:
if (!empty($_POST["name"]) && !empty($_POST["roll"])){

Is it possible to override a page with another include?

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'])) {

How to use variable inside parameter $_GET? example: ($_GET[$my_var])

I'm developing a plugin for wordpress, the parameter of the $ _GET is recorded in the database according to the preference of the User via the Wordpress Admin Panel. The following validation has to be via the $ _GET, this is the function:
$db_url = get_option('my_get_url');
// returns the value of the database entered by User
// on this case return --> page=nosupport
$url_explode = explode("=", $db_url);
$url_before = $url_explode[0]; // --> page
$url_after = $url_explode[1]; // --> nosupport
echo "Before: ".$url_before; // here are ok, return --> page
echo "After: ".$url_after; // here are ok, return --> nosupport
My problem is here:
// here $_GET no have any value, dont work on validate...
if($_GET[$url_before] != ""){
if($_GET['$url_before']=="nosupport"){
// my function goes here...
}
}
I using for test the parameter:
echo $_GET[$url_before];
But dont return any value...
I found the problem, i had already tested all of these options, but ever dont working, the problem was that I was testing the function inside the main page of my site, and on the main page (mysite.com) does not get the parameter (?page=nossuport), so always returning null values​​, when I used the variable in the GET or used the echo $GET[$my_var] to test.. It was a great carelessness of mine, would never work...
by the way, the two parameters works correctly:
$_GET[$url_before]
$_GET["$url_before"]
The Problem are solved, Thanks for help.
if($_GET[$url_before] != ""){
if($_GET[$url_before]=="nosupport"){ // note no "" here
// my function goes here...
}
}
In your solution, the key was treated as a string, with no variables evaluated.
You forgot to take out the ' in the second condition.
You wrote:
$_GET['$url_before']
I'm guessing it should be:
$_GET[$url_before]
foreach($_GET as $key => $value){
if($key == "nosupport"){
}
}

Categories