PHP - Hyperlink not getting full string [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have following code but unable to get full variable values on return.
<?php
$no7="Girls pink";
$no8="All Categories";
$aw="2";
echo'<table width="auto" border="0">
<tr>
<td width="auto" class="s">'; echo "".$aw.""; echo'</td>
</tr>
</table>';
?>
Thanks for help Regards,

Add quotes on href value:
echo "<a href='search.php?search_name=".$no7."&mydropdown=".$no8."&pno=".$aw."'>".$aw."</a>";
// ^--------------------------- quotes ----------------------------^

Use urlencode and htmlspecialchars:
echo "<a href='search.php?search_name=" . urlencode($no7) . "&mydropdown=" . urlencode($no8) . "&pno=" . urlencode($aw) . "'>" . htmlspecialchars($aw) . "</a>";
Also: add quotes around the URL, and replace & with & for better HTML syntax.

Clean up the horrible formatting/layout and use a HEREDOC:
echo <<<EOL
<table width="auto" border="0">
<tr>
<td width="auto" class="s">{$aw}</td>
</tr>
</table>
EOL;
Note how the HEREDOC removes the need for all those individual echoes, allows for variable interpolation into the string (no more 'break out of string to concatenate'), and allows for proper formatting/indentation of the HTML code.

The problem is the spaces in your variables. It's more of an issue with browsers interperting spaces as the end of your href tag. As suggested earlier, use urlencode or convert the spaces in your variables to +

Related

Echo a string literal from PHP that includes both quotes and apostrophes

I have a little bit more complicated row than I can handle.
I am echoing and I got lost in the " ', could someone help me out and tell me how to have this line correctly?
echo '<td style="text-align: center"><a onclick=" window.open('/edit.php?id=' . $row['id'] . ','_self')"><img height="30" width="30" src="/wp-content/themes/sparkling/edit.png"/></a></td>';
Escape single quotation?
echo '...<a onclick=" window.open(\'/edit.php?id=...';
Edit
To show single quotation in string wrapped with single quotation, you need to escape it, like this
echo 'Hello \' world';
So your code should be
echo '<td style="text-align: center"><a onclick=" window.open(\'/edit.php?id=' . $row['id'] . '\',\'_self\')"><img height="30" width="30" src="/wp-content/themes/sparkling/edit.png"/></a></td>';
Use the heredoc syntax to create the string and your " and ' can be used freely inside the string.
See manual about heredoc http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Example of heredoc:
https://3v4l.org/SJTBO
I don't know about a third quotation mark in code.
But, using "heredoc" syntax is really usefull when you want/need to keep both the single and double quotes unescaped.
Using it, your code should look like this:
echo <<<EOD
<td style="text-align: center">
<a onclick="window.open('/edit.php?id='{$row['id']}', '_self')">
<img height="30" width="30" src="/wp-content/themes/sparkling/edit.png"/>
</a>
</td>
EOD;
Think about using { } around your variables to make them more visible.
See documentation about it here: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

How to do href in php

At the beginning I would like to say that this is my first post and I apologize in advance for any possible mistakes. And these are my beginnings in programming.
I have a problem with appropriate action download.php in the following code snippet:
<?php
$cv= $row[25];
$output .= '
<tr>
<td> <a href='download.php?dow=$cv'>Download</a> </td>
</tr>
';
?>
I wish it worked like the following code (in which it operates correctly):
<tr>
<td><?php echo "<a href='download.php?dow=$cv'>Download</a><br>"; ?></td>
</tr>
You need to concatenate the string. To concatenate you use a dot . This will put the variable value inside your string.
<?php
$output .= "
<tr>
<td><a href='download.php?dow=".$cv."'>Download</a> </td>
</tr>
";
you closed string before it's even finished!
the $output is a variable and you tried to store the HTML as an string inside it, but you've closed the ' sign before the download.php?dow=$cv and then opened it again. the correct way to write it is:
<?php
$cv= $row[25];
$output .= "
<tr>
<td> Download </td>
</tr>
";
?>
the \ before " called escaping and is needed because we dont want to end our string! just want to add that quote sign as a character like the others.

browser is not showing html tags in inspect element [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have the following PHP code. I don't know what's wrong with it, but the browser does not show the table in the inspector.
<main class="container clear">
<?php
require 'connect.inc.php';
$query = "SELECT * FROM `names`";
$name = mysql_query($query);
$namelist = mysql_fetch_assoc($name);
?>
<tr>
<td><?php echo $namelist['name']; ?></td>
<td><?php echo $namelist['designation']; ?></td>
<td><?php echo $namelist['phone']; ?></td>
<td><?php echo $namelist['email']; ?></td>
</tr>
</main>
How can I fix it?
It doesn't show a table because you are missing the <table> tag.
You'd do it like this:
.. code ..
?>
<table>
<tr>
.. code ..
</tr>
</table>
</main>
Since you have a a query like SELECT * FROM names, I assume you want to show all the values, so your code should look like this:
<?php
require 'connect.inc.php';
$query = "SELECT * FROM `names`";
$name = mysql_query($query);
?>
<main class="container clear">
<table border="1"> <!-- border just for you to see it -->
<tr>
<th>Name</th>
<th>Designation</th>
<th>Phone</th>
<th>Email</th>
</tr>
<?php
while($namelist = mysql_fetch_assoc($name);
echo '<tr>';
echo '<td>' . $namelist['name'] . '</td>';
echo '<td>' . $namelist['designation'] . '</td>';
echo '<td>' . $namelist['phone'] . '</td>';
echo '<td>' . $namelist['email'] . '</td>';
echo '</tr>';
}
?>
</table>
</main>
Side notes:
That table name is probably not the best option, specially for the fields (name, designation, phone, email) it has. Try using a name that is more suggestive.
Oh.. and please, stop using mysql_* functions. This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

printing php variables in echo ''

i have a big table where should print 5 php variables.
I am printing this way
echo '
<td valign="center" align="center" colspan="2">Number : <br/>$num</td>
';
but it prints $num not the value of $num
EDIT :
I realized that.
echo '
' <td valign="center" align="center" colspan="2">Number : <br/>'.$num.'</td> ';
Thank you for help :)
Single quoted PHP strings do not interpolate variables. Double quoted strings do. Use a double quoted string.
Better yet, don't build up HTML in strings in the first place. Drop into PHP mode only when you need access to PHP.
<td valign="center" align="center" colspan="2">Number : <br/><?php echo $num;?></td>

Correct syntax using JS 'onclick' functions inside PHP echo statements AND with embedded XML fetching

I am likely to get b'*ch slapped because I haven't searched the forum enough before posting, but I really think I searched all relevant posts already, many seem not specifically covering the question I have, the others fly right over my beginner's head ( as I am new to PHP & js ). That being said...
I have built a PHP code to fetch data from an XML file using the $xml=simplexml_load_file();
No worries there, however one of the needed data 'entries' or 'fields' needs to exist within an onclick and/or an onmouseup javascript function.
The code is as follows:
<?php
$xml=simplexml_load_file('prod_test_feed_sameday.xml');
$max = 8;
$i = 1;
foreach($xml->product as $feed){
if ($i <= $max){
echo "<table id='{$feed->position}' class='prod_container'>
<td class='hidden_mask' id='{$feed->position}'>
</td>
<td class='hidden_image' id='{$feed->position}'><span style='background:url({$feed->prod_image_large}) no-repeat center;'/></span><div><a onmouseup='$('.hidden_image#{$feed->position}').slideToggle('slow');' onclick='$('.hidden_mask#{$feed->position}').hide();'><b>CLOSE</b></a></div>
</td>
<tr>
<td class='prod_image' id='{$feed->position}'>
<span style='background:url({$feed->prod_image_small}) no-repeat center; background-size:contain;'/></span>
</td>
<td rowspan='1' class='info_toggle' id='{$feed->position}'><a onmouseup='$('.hidden_image#{$feed->position}').slideToggle('slow');' onclick='$('.hidden_mask#{$feed->position}').show();><img src='images/zoom_02.png' title='See a larger image of these flowers' /></a>
</td>
</tr>
<tr>
<td colspan='2' class='prod_name' id='{$feed->position}'>{$feed->prod_name}
</td>
</tr>
<tr>
<td colspan='2' class='prod_price' id='{$feed->position}'><span id='{$feed->position}'>from: £{$feed->price}</span><a href='{$feed->prod_link}' target='_blank'><span class='buy_button'> Buy it now! </span></a></td>
</tr>
</table>";
$i++;
}
}
?>
The data and the CSS all work perfectly. There is a href link towards the end of the code, which also works perfectly.
I am racking my brain trying to find the error in my syntax within the onlick function.
I have tried all manners of backslashing, using trial and error, for exampel:
onclick='$(\'.hidden_mask#{$feed->position}\').hide();' or
onclick='\'$('.hidden_mask#{$feed->position}').hide();\'' or
onclick=\''$(\'.hidden_mask#{$feed->position}\').hide();\'' or even
onclick=\''$(\\'.hidden_mask#{$feed->position}\\').hide();\'' <--Freddy Krugar 'slashing' example
At any rate I am at a loss.
Try with double quotes and escape them:
echo " ...
onclick=\"$('.hidden_mask#{$feed->position}').hide();\"
...";
Or
echo " ...
onclick='$(\".hidden_mask#{$feed->position}\").hide();'
...";
DEMO
This way you do the escaping in the PHP only, without needing the Freddy Krugar escaping for the DOM parser.

Categories