How to set a PHP variable via URL? - php

I'm trying to cause a variable $btn1Pressed to be set via a URL load. For example, loading http://mywebsite.com/myphp.php?btn1Pressed=1 would set the variable to 1. The below test code doesn't seem to be doing anything:
<?php
if ($btn1Pressed == 1) {
echo 'Button One Pressed';
}
else{
echo 'Button Two Pressed';
}
?>

Before the vampires arrive...
<?php
if (isset($_GET['btn1Pressed']) && $_GET['btn1Pressed'] == 1) {
echo 'Button One Pressed';
}
else{
echo 'Button Two Pressed';
}
?>
Anything in the query string will be in PHP's $_GET array. To see the entire array you can print_r($_GET); in your PHP code. In the example I am also testing to make sure the variable has been set, for safeties sake. You should never accept user input without sanitizing, which I have not done here.
You can also set a variable with the array item:
$btn1Pressed = $_GET['btn1Pressed'];

You can try use GET method:
if ($_GET['btn1Pressed'] == 1) {
echo 'Button One Pressed';
}
else{
echo 'Button Two Pressed';
}

<?php
$btn_pressed = filter_input(INPUT_GET, 'btn1Pressed', FILTER_SANITIZE_NUMBER_INT);
if ($btn_pressed == 1) {
echo 'Button One Pressed';
} else {
echo 'anything';
}
?>

Related

If session has value echo it else echo "all"

Update 2
I'll just leave this here for future references. But this is the solution I created with all the help I got here. Thanks!
<script>
function Refresh() {
location.reload();
}
</script>
<?php $number = $_SESSION["page_id"]; ?>
<?php
if (isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo do_shortcode('[RICH_REVIEWS_SHOW category="page" num="all" id="'. $number .'"]');
session_destroy();
echo ('<button class="btn btn-0001" onclick="Refresh()">Show All</button>');
}
else{ echo do_shortcode('[RICH_REVIEWS_SHOW num="all"]'); }
;
?>
Update
So I'm trying to just do a simple echo to see if the session is set using this code:
<?php if(isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo 'Set and not empty, and no undefined index error!');
};?>
But doing this breaks my page, I just get a blank page? How do I check if the session is set? When I do a echo of the session using this code:
<?php echo $_SESSION["page_id"]; ?>
It does output the correct session value?? What am I doing wrong?
I have a sessions saved with PHP and I'm using this so that the page ID from Wordpress is echo-ed in a shortcode do_shortcode('');
This is what my code looks like:
<?php $number = $_SESSION["page_id"]; ?>
<?php echo do_shortcode('[RICH_REVIEWS_SHOW category="page" num="all" id="'. $number .'"]'); ?>
<?php echo do_shortcode_all('[RICH_REVIEWS_SHOW category="page" num="all" id="all"]'); ?>
<?php echo $shortcode ;?>
<?php echo $shortcode_all ;?>
Now, what I would like to do is IF the page_id is not stored in the session it should echo all. So how do I go about this?
I found this code, and I think its something I need... But I'm not that great a programmer/coder...
<?php
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
?>
So, if I where to put these two together I would get something like this
<?php
$number = $_SESSION["page_id"];
// Evaluates to true because $var is empty
if (empty($number)) {
echo $shortcode_all ;
}
// Evaluates as true because $var is set
if (isset($number)) {
echo $shortcode ;
}
?>
Am I in the right direction?
Solution
<?php
if (isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo('Set and not empty, and no undefined index error!');
};
?>
What am I supposed to feel from the missing (?
If you are using wordpress then you should use the session_start(); in wp-config.php file so please first put in you wp-config.php at top and then check.
<?php
$number = $_SESSION["page_id"];
// Evaluates to true because $var is empty
if (empty($number) || $number=='') {
echo $shortcode_all ;
}else{
echo $shortcode ;
}
?>

If statement within echo?

I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;

Passing a PHP Value from a link

I am new to PHP and learning. I'm trying to pass a value through a url link but it doesn't seem to work.
The link value I am passing is http://www.mysite.com/index.php?id=f
I want to run a js script if ID not F seen below but right now when I run it. It doesn't do anything:
<?php
$ShowDeskTop = $_GET['id'];
if (isset($ShowDeskTop)){
echo $ShowDeskTop;
if ($ShowDeskTop != "f"){
echo "ShowDeskTop Value is not F";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
};
};
?>
I know this is easy PHP 101 but I can't figure it out. I have tried everything from w3schools to other sites on Google for the answer and having no luck. Could someone please tell me what I am doing wrong?
Thank you!
$ShowDeskTop is not the same as $ShowDesktop variables names are case sensitive!
This is never gonna work since you set the variable AFTER checking if it exist..
The most easy way:
<?php
if (isset($_GET['id'])) {
echo $_GET['id'];
if ($_GET['id'] != 'f') {
?>
<script type="text/javascript">
if (screen.width < 800) {
window.location = "../mobile/index.php";
}
</script>
<?php
}
}
?>
I don't think <> is valid in PHP (it is in VB.NET ..) the is not operator is != or !== (strict/loose comparison).
Also you don't have to close if statements with a ;
This:
if (expr) {
}
Is valid and not this:
if (expr) {
};
I thought about writing != instead of <>.
You have a number of problems including bad variable case (i.e. variables not matching), checking for variables before they exist, etc. You can simply do something like this:
if (!empty($_GET['id'])) { // note I check for $_GET['id'] value here not $ShowDeskTop
$ShowDeskTop = $_GET['id'];
echo $ShowDeskTop; // note I change case here
if ($ShowDeskTop !== "f"){ // note the use of strict comparison operator here
echo "YES, the id doesn't = f";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
} // note the removal of semicolon here it is not needed and is bad coding practice in PHP - this is basically just an empty line of code
} // removed semicolon here as well
Fist thing, you need ; at the end of echo $ShowDesktop
And, what does f mean in if ($ShowDeskTop <> "f"){
use strcmp() instead of <> operator.
Try
if(!strcmp($ShowDeskTop, "f")){
echo "YES, the id doesn't = f";
}
<?php
$ShowDeskTop = $_GET['id']; // assign before checking
if (isset($ShowDeskTop)){
//echo $ShowDeskTop;
if ($ShowDeskTop !== "f"){
echo "YES, the id doesn't = f";
echo "<script type='text/javascript'>";
echo "if (screen.width<800)";
echo "{";
echo "window.location.replace('../mobile/index.php');"; // assuming your path is correct
echo "}";
echo "</script>";
}
}
?>

PHP function that decides which HTML code to use?

I'm looking for a function like and if else statement for php which will execute certain html code.
For example:
<?php>
$result = 1;
if ($result == 1)
<?>
html code
else
html code
So, based off the result variable gotten from php scripts, a certain html page is output. I've tried echoing the entire html page, but it just displays the html code-> tags and such.
Hopefully you get what I'm trying to get across, ask if you need any clarification questions. Thanks!
That should work:
<?php
$result = 1;
if($result==1) {
?>
html code
<?php
} else {
?>
html code
<?php
}
?>
The problem I'm facing with the if else statement, is in order to display the html, I have to exit php coding. Thus, the if else statement will not work. (Link)
This is not entirely true. You can use the approach below:
<?php
// Do evaluations
if ( $result == "something" )
{
?>
<p>Example HTML code</p>
<?php
} elseif ( $result == "another thing")
{
?>
<span>Different HTML code</p>
<?php
} else {
?>
<h4>Foobar.</h4>
<?php
}
// Rest of the PHP code
?>
Or, if you don't want to exit PHP coding, you can use the echo or print statements. Example:
<?php
// Evaluations
if ( $result == "foo" )
{
echo "<p>Bar.</p>";
} else {
echo "<h4>Baz</p>";
}
// Some else PHP code
?>
Just be careful with proper sequences of ' and " characters. If your HTML tags are to have arguments, you should watch your step and use either of the following approaches:
echo "<span class=\"foo\">bar</span>";
echo '<span class="foo">bar</span>";
If you want to evaluate some PHP and print the HTML results later, you could use something like this
<?php
$output = "";
if ( $result == "something" ) {
$output = '<p>Example HTML code</p>';
} else if ( $result == "another thing") {
$output = '<span>Different HTML code</p>';
} else {
$output = '<h4>Foobar.</h4>';
}
// Output wherever you like
echo $output;
?>
EDIT (because I'm not sure what you;re trying to do so i'm just putting out different ideas):
If you're trying to output an entire page, it may be useful to use header('location: newPage.html'); instead of $output. This redirects the browser to an entirely new web page. Or you can likely include newPage.html as well.
very close:
<?php
$result = 1;
if ($result == 1){
?>
html code
<?php } //close if
else {
?>
html code
<?php
} //close else
?>
you can echo html code something like this
<?php
$result = 1;
if ($result == 1){
echo "<h1>I love using PHP!</h1>";
}
?>
this would output if Result is 1
**I love using PHP!** //but slightly bigger since its H1

Need help in display members name when logged in using PHP?

for some r.eason I cant display a logged in users name when they are logged in? the code is below
<?php
if (isset($_SESSION['user_id'])) {
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else { echo 'something';
}
?>
Thanks every one but i solved it.
Ack! Just look at your code. Do you know what this line is doing?
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
That's so wrong I don't even know where to begin. Just try
echo $_SESSION['first_name'];
And see if that gets you closer to what you want ;)
Make sure you're also calling session_start() before trying to access the variables.
Change your code to:
<?php
session_start();
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", " . $_SESSION['first_name']} . '!';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?>
This is not a valid PHP code. Single quote "'" are not pair up. The block ('{' and '}') are also not pairing up.
The most importantly, the code to show the first name is in a string so it will not be shown.
I think the code you are trying to write is:
<?php
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", {$_SESSION['first_name']}!";
}
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?&gtl
Is it?
Here are the list of possibilities of the mistakes and make sure that you have corrected them
1) have you set the cookie "first_name" using setcookie method...?
2) Then have u called the session_start() function so that the session variables can be called in that page??
3) Try echo $_SESSION['first_name']... i don understand why you have put the flower brackets coz i never have used them even once in my 15 php projects..

Categories