How to add a variable to this? - php

I am using the following snippet. How do I add q=$k in the <a href url>?
<?php
echo '<a id=d href="'.$_SERVER['PHP_SELF'].'?pg='.($s+10).'">Next</a>';
?>

<?php
echo '<a id=d href="'.$_SERVER['PHP_SELF'].'?pg='.($s+10).'&q='.$k.'">Next</a>';
?>
Something like that?

echo '<a id=d href="' . $_SERVER['PHP_SELF'] . '?pg=' . ($s+10) . '&q=' . $k . '">Next</a>';

You can do this:
echo "<a id='d' href='" . html_entities("$_SERVER[PHP_SELF]?pg="
. urlencode($s + 10) . "&q=" . urlencode($k)) . "'>Next</a>";
I added suitable escaping for the URL parameter and HTML output: URL encoding for the URL GET parameters, and HTML entity replacement for the entire URL string.

Related

php string not working properly

My code in php is
echo $preference[0]."</br>";
echo '<p class="filter_entity">'.$preference[0].'<img src="' . base_url() . 'images/close-icon.png" onClick="filter_close()"></p>';
html out put is
Business
<img onclick="filter_close()" src="http://localhost/AEC/images/close-icon.png">
I want to put $preference[0] in filter_close() of my php code ... dont know how to do it .
Something like this?
$param = $preference[0];
echo '<p class="filter_entity">'.$preference[0].'<img src="' . base_url() . 'images/close-icon.png" onClick="filter_close(\''.echo $param.'\')"></p>';
Just append the variable as a parameter of the js function
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
echo "Some array item: {$array[0]}";

using an image as a link (php)

can anyone tell my why this:
echo "<a href='productdetail.php?product=" . $rij['productnaam'] . "'><img src='../images/producten/monster_energy_green.png' alt='' /></a>";
does give a clickable image link, and this:
echo "<a href='productdetail.php?product=" . $rij['productnaam'] . "'><img src='" . $rij['afbeelding_klein'] . "' alt'productafbeelding' /></a>";
doesnt? (not clickable).
thanks in advance!!
php rookie..
In the first one you have a real scr it's directing to with a file extension. the second does not. If the second is supposed to be picking up a variable we would need more information
echo "<a href='productdetail.php?product=" . $rij['productnaam'] . "'><img src='" . $rij['afbeelding_klein'] . "' alt='productafbeelding' /></a>";
just add a "=" between alt and 'productafbeelding'
Try this one:
echo '<img src="' . $rij['afbeelding_klein'] . '" alt="productafbeelding" />';

defining POST variable as href link, target='blank'

I am trying to edit this code to make it a link which opens ins a new tab, but wherever I insert the "target='blank'" part in the code I get a parse error, can anyone one help me out please. Thanks.
$websiteurl = "" . $_POST['websiteurl'] . "";
need to add:
target='blank'
$websiteurl = '<a target="_blank" href="' .
$_POST['websiteurl'] . '">' . $_POST['websiteurl'] . '</a>';
You missed _.
put target="_blank"
Mine is like this:
$output .= "<p class='fb-page-name'><a href='". $app['link'] ."' title='". $app['name'] ."'>". $app['name'] ."</a></p>\n";
I had to use target='_blank' instead of target="_blank".
I'm just saying because maybe someone is struggling with it as I was.
$websiteurl = '' . $_POST['websiteurl'] . '';

How would you do a code-escape in PHP?

I have a huge list of stuff for a glossary ( about 17 pages worth ) that I have to put into an XML file. So I decided I'd use php to make it. My code works, except where ALL the XML code is, it doesn't show because it's trying to render it. Help?
$arg=explode("\n", $strang);
echo count($arg);
for ($i=0;$i<=count($arg);$i=$i+3)
{
echo "<word id='" . $arg[$i+1] . "'>";
echo "<desc>" . $arg[$i] . " - " . $arg[$i+2] . "</desc>";
echo "<pic></pic>";
echo "<audio></audio>";
}
I assume by render it you mean in your browser? If so, you'll need to escape the characters so they will be interpreted literally rather than as markup.
Check out htmlspecialchars and htmlentities
use CDATA construction:
echo "<desc><![CDATA[" . $arg[$i] . " - " . $arg[$i+2] . "]]></desc>";
If this is your entire script, fastest way would probably be to swap all of the <'s with <
$arg=explode("\n", $strang);
echo count($arg);
for ($i=0;$i<=count($arg);$i=$i+3)
{
echo "<word id='" . $arg[$i+1] . "'>";
echo "<desc>" . $arg[$i] . " - " . $arg[$i+2] . "</desc>";
echo "<pic></pic>";
echo "<audio></audio>";
}

correct way to echo a link with a onclick javascript function

my question is how I can echo this the right way
because the variable in the onclick function gives out a undefined error
$openchat="<a href='javascript:void(0)' onClick='return chatWith(" . $livenaam .")'>" . $livenaam . "</a><br>";
echo $openchat;
I want to use it in a loop to get a list off users online for the chat
Thanks, Richard
Looks like you are missing some quotes:
$openchat="<a href='javascript:void(0)' onClick='return chatWith(\"" . $livenaam ."\")'>" . $livenaam . "</a><br>";
or for increased security:
$openchat="<a href='javascript:void(0)' onClick='return chatWith(\"" . htmlspecialchars($livenaam,ENT_QUOTES) ."\")'>" . htmlspecialchars($livenaam,ENT_QUOTES) . "</a><br>";
Try this:
'' . htmlspecialchars($livenaam) . '<br>'
If json_encode is not available, try this:
'' . htmlspecialchars($livenaam) . '<br>'

Categories