Show an Image if a PHP argument proves true - php

I barely know how to use PHP and I can't seem to make my code show an image if a condition proves true. This is the code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
}
?>
I tried writing this thinking it would work and it didn't:
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg'>";
Also I tried a code I found which started with the function: header() but it caused a tremendously long error, which said something like header already defined.
Thanks

You have used 'double quotes' incorrectly in the echo statement.
Try the following:
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg' alt='Preview not available' />"
Regards,
Mahendra Liya.

You should var_dump($first) to know what it contains
check if the condition is really getting true
and also put single quote inside the double quote.
if ($first=="#"){
echo 'yes it is true';
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg'>";
}
close the img tag

The part of the query string starting with # (so-called "hash") is not being sent to the server. That is, if your page is called like myblog.com/foo?bar=baz#quux, you php script will only receive myblog.com/foo?bar=baz. You need javascript if you want to handle urls with hashes.

Related

Getting a variable from a link PHP

I have two files, one called test3.php, and another called test4.php. I'm trying to echo the variable in the link of the file test4.php, but it's echoing unexpected results. Please take a look.
In the file called test3.php:
<?php
$text = "Good morning.";
header('Location:test4.php?text=$text');
?>
In the file called test4.php:
<?php
$text = $_GET['text'];
echo "$text";
?>
Expected echo result:
"Good morning."
Actual echo result:
$text
I don't understand why it's echoing out $text, instead of "Good morning." One thing that came to mind is that you can't actually set variables when you're using a header, so if that's the case please let me know. Thank you.
Variables do not get parsed in single quotes
header('Location:test4.php?text=$text');
therefore, you need to use double quotes
header("Location:test4.php?text=$text");
References:
https://php.net/language.types.string
https://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
What is the difference between single-quoted and double-quoted strings in PHP?
Plus, it's best to add exit; after header, in order to stop further execution, should you have more code below that (or decide to in the future).
http://php.net/manual/en/function.header.php
and using a full http:// call, as per the manual
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
Footnotes, about header, and as per the manual:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
However you wrote, and I'm using this literally:
Expected echo result:
"Good morning."
If you want to echo just that "Good morning." having the text in double quotes, then you will need to change the following in your test4.php file:
echo "$text";
to, and escaping the " using \
echo "\"$text\"";
use
header("Location:test4.php?text=".$text);
In test4.php:
<?php
$text = $_GET['text'];
echo "$text";
?>
When you quote "$text", you are echoing af string.
What you will want to do, is echo the variable: $text.
So:
<?php
$text = $_GET['text'];
echo $text;
?>
...Without the quotes.. :)
And also, the: header('Location:test4.php?text=$text'); is a bitch, if you use it below a lot of code...
Safe yourself some trouble, and use:
echo "<script type='text/javascript'>window.location.href = 'test4.php?text=".$text."';</script>";
instead ;)

PHP echo changing text?

I'm a beginner PHP programmer, and I was wondering what was wrong with my code.
Here is the small excerpt from the affected spot:
echo "<form action='?tab=4' name='toedit5' method='get'><input value='text' onblur='edit('toedit5')' /></form>";
In Chrome's Developer Tools, the form element totally disappears, and the edit('toedit5') becomes edit(' toedit5').
The edit() function doesn't execute.
Is there anything wrong with this one line of code? Otherwise it is outside code messing with it. Sorry I didn't include it, but I don't know what to include. If you need more information, please tell me.
Thanks.
You need to escape your quotes inside your quoted echo'd statement, like this:
<?php
echo "<form action='?tab=4' name='toedit5' method='get'>";
echo "<input value='text' onblur='edit(\"toedit5\")' />"; // escaped..!
echo "</form>";
?>
It helped me to think about it like this when I was starting out: how does your browser know if the second single quote in onblur='edit('toedit5')' is closing your onblur statement or opening up the parameter? In this example, your browser will pair up the first 2 quotes it sees and assign that to the onblur attribute, i.e.: onblur='edit(' only!
Update 1:
Using the code above, I inspected a quick PHP page I created in Chrome's developer tools and was able to see the following (form available for inspection):
You really should use the more standard double quotes around the HTML properties and use single quotes around your string, with escaped single quotes within the javascript method calls. Like this:
echo '<form action="?tab=4" name="toedit5" method="get"><input value="text" onblur="edit(\'toedit5\')" /></form>';

PHP echo-ing a PHP code inside an echo

I'm quite new here. I'm trying to make a blog/journal site that allows users to post their own journal. I'm still quite reluctant on making it because I am really afraid of malicious code injections.
So here's a sample code:
<?php
$test = "<b>blah</b>"; //User input from SQL
echo "$test";
?>
What will come out is just the word "blah" in bold right? What I was trying to achieve was to echo "<b>blah</b>" instead. I don't want people to put some PHP codes that can actually mess up my whole web page. Please keep in mind that the variable $test is actually a MYSQL query, so that variable will be needed as an example. I know you can do echo '$test'; but it just comes out as "$test" instead. I feel like pulling my hair out I can't figure it out yet.
The second solution I know of is the htmlspecialchars(); function, but I want the strings to display as what I typed, not the converted ones...
Is there any way I can do that?
I think the OP wants the HTML itself to be output to the page, and not have the tags stripped. To achieve this, you can run the string first through htmlentities()
$test = '<b>blah</b>';
echo htmlentities($test);
This will output:
<b>blah</b>
Which will render in the page as
<b>blah</b>
Echo don't execute PHP code from string. This is impossible and this is not security hole in your code.
You can use a template engine like Twig for exemple.
If htmlspecialchars(); is not the one you are looking for, try the header() option.
header('Content-type: text/plain');
When you are gonna give <b>Hi</b> to a browser, it will be displayed in Bold and not the text be returned. But you can try this way, outputting it inside a <textarea></textarea>.
Or the other way is to use htmlentities():
<?php
$test = "<b>blah</b>"; //User input from SQL
echo htmlentities("$test");
?>

Displaying an image from a directory in PHP

I'm trying to display an image file from a directory using a PHP echo command and an IMG tag.
Here is the code:
//These variables represent the file name extensions from a form element from a previous page
$bannerimg=$_POST["banimg"];
$adimage=$_POST["adimage"];
echo "<img src='imgdir/'".$bannerimg."/>";
When I echo out the file variables ($bannerimg and $adimage) I get the proper file name and extension.
In theory, will this work? If so, what is the proper syntax to handle that echo statement?
Thanks for all the help.
Dustin
Yes it would work, but you should have tested that already.
Alternative syntaxes to the echo statement that I find a little bit more readable would be:
echo "<img src='imgdir/{$bannerimg}/>";
echo "<img src='imgdir/$bannerimg/>";
You can read all about variable parsing in the manual, the first syntax is the complex one and the second the simple. I prefer the complex one as the end of the variable is clearly defined and you can use it for complex expressions, not just simple variables.
You're doing it right but you can use the following just to keep it clean.
echo "<img src='imgdir/$bannerimg' />";
It should work.
To Sandeep's comment, I would have gone the other way.
echo '<img src="imgdir/'.$bannerimg.'/>';
Using " means the parser needs to check to see if there is anything to evaluate.

Img src as a php variable

What Im trying to do is write a script that grabs the url of thumbnails attached to posts in wordpress. It sounds really easy(as I'm sure the solution is) but I can't seem to get it to work, I keep getting syntax errors no matter what I try. The problem line is the second echo(Img src...). Any help would be greatly appreciated.
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'archive-thumb');
$image_url = $image_url[0];
echo "<li class=\"recent-img-widget-li\"><a href='".get_permalink()."'>;
echo "<img src=\"".$image_url."\" width=\"120\" height=\"120\">";
echo "</a></li>";
Simply enough, you're not closing your first string after get_permalink(). Yo need another quote after the >.
You never close the first string. You just need a quote before the greater than on the first line (and possibly the second?). Look at the syntax highlighting that SO has.
A general guideline is to always look at the row above the one that is giving the error.
In this case you have forgotten to end the string in the last part of the first echo statement.
...ermalink()."'>;
Should be
...ermalink()."'>";
For one you should close that first echo. Missing the closing "

Categories