Syntax Error in concat echo statement and html code - php

Please help me to understand syntax of concat following
i want following code inside echo to understand concate
1--> <div class="alert echo $_REQUEST['error_id'];?>"\>
I try following code but getting syntax error that there is two echo..
2--> echo "<div class=\"alert" . echo $_REQUEST['error_id']; . "\">";
where mistake is that i cant get it... and i want two answer both using single quote and double quote
EDIT
Thank #Rizier123 now it working but css is not working as i have apply in calss="alert"
while using following code its working fine
<div class="alert echo $_REQUEST['error_id'];?>"\>
But after applying your code its not working..only text appear but background color is not comming nad boarder is also dissappear as below
class name is alert shows the status of login..
EDIT
Thanks again i just forget to put space after class name..

You don't need to write echo and ; again!
So this should work:
//echo with double quotes
echo "<div class=\"alert" . $_REQUEST['error_id'] . "\">";
//echo with single quotes
echo '<div class="alert' . $_REQUEST['error_id'] . '">';
Maybe you need to add a space if your class name includes spaces!(..."alert "...)

try this
echo '<div class=\"alert"' . $_REQUEST['error_id'] . '">';

You can do either 2 ways
1. PHP in HTML
<div class="alert<?php echo $_REQUEST['error_id'];?>">
2. HTML in PHP
echo "<div class='alert". $_REQUEST['error_id'] ."'>";
Or you can also do like
echo "<div class=\"alert". $_REQUEST['error_id'] ."\">";

Related

My onclick events in my php include file do not work

I am working on my college web design course during my easter break and have run into a problem...
I have a menu on each page that I have removed from the html pages and written into a php file and that is used with an include in each page. Everything works fine apart from the onclick events that when clicked SHOULD load another page but at the moment does not do anything.
Here is my php file...
<?php
echo "<div id='leftPnl'>";
echo "<div class='navcon'>";
echo "<button class='static'
onclick='window.location.href='"."./index.html'".">Home</button>";
echo "<button class='accordion'>Gallery</button>";
echo "<div class='panel'>";
echo "<button class='static' onclick='window.location.href='"."./html/countryside.php'".">Countryside</button>";
echo "<div id='socialmedia'>";
echo "<img src='../images/sitewide/map.png'>";
echo "<br>Contact us on...<br>";
echo "<a class='fab fa-facebook-square fa-2x'
onclick='msg('"."Facebook"."')"."></a>";
echo "<a class='fab fa-instagram fa-2x' onclick='msg('"."Instagram"."')".">
</a>";
echo "<a class='fab fa-twitter-square fa-2x'
onclick='msg('"."Twitter"."')"."></a>";
echo "<a class='fab fa-tumblr-square fa-2x' onclick='msg('"."Tumblr"."')".">
</a>";
echo "<div id='clock'>";
echo "<p class='date'>{{ date }}</p>";
echo "<p class='time'>{{ time }}</p>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
on running the file and inspecting the element in Chrome, the url that the href uses is not formatted correctly the ./ are missing and a single quote is also missing.
Is it simply that I am getting confused with the position of my single and double quotes in the php?
Thanks.
PS, I apologise if this has already been asked - I looked and could not find the answer.
Is it simply that I am getting confused with the position of my single and double quotes in the php?
Yes.
Look at the output of the PHP. To take one example:
onclick='msg('Facebook')>
You start the attribute value with '.
Then you try to use ' inside it.
Then you forget the ' at the end!
There is no reason to nest any of this code inside PHP string literals. All that achieves it making it harder to debug.
Just write the HTML directly, outside of <?php ... ?> sections.
It wouldn't be a bad idea to get rid of the intrinsic event attributes too, and use JavaScript to bind event handlers.

PHP create Url in mysqli_query while loop for every record

I would like to create a link within a while loop where the anchor link is a record from the query and the href passes a variable from the same query to another page so I only have to create one page that displays information based on the passed variable.
echo "<td>";
echo ''$row['result']''';
echo "</td></tr>";
This link kills my page and return an error.
echo "<td>";
echo ''.$row['result'].'';
echo "</td></tr>";
Should fix your code your quotes are not closed right
Your href string concatenation is incorrect.
It seems like you tried closing your strings and concatenating them halfway through and then stopped. Even with Stack Overflow you can see the string errors.
This is how your href echo should look:
echo '<a href="stats_game.php?idGame=' .$row['idGame'] . '>' . $row['result'] . '</a>';
It works fine for me
echo "<td><a href='stats_game.php?idGame=".$row['idGame']."'>".$row['result']."</a></td>";

php echo href from mysql issue

Ok, so I have the php echo working and pulling specific url's from the mysql database but this string that I got from an example is adding in single quotes to my href. So instead of being localhost/newdesign/about.php it is localhost/newdesign/'about.php'.
here is the code:
<p>
<?php
echo '' . $row['url'] . '';
?>
</p>
Thank You
It would be simpler if you use <?php echo only around the variables, not the literal HTML parts as well. It also looks like part of the onclick got lost when you were copying to SO.
<p><?php echo $row['url'] ?></p>
No need for the 2nd apostrophe.
"' . $row['url'] . '"

PHP : echo a href

I have this php line which works fine:
echo "<p>" . $post['message']. "</p>";
But I want to change it so it will link to my page (not to a single post). So it should look like that.
echo "<p>" . $post['message']. "</p>";
I have tried a lot many proposition gathered on different website, but each time I am getting an error.
Any idea ?
Thanks a lot!
Using single and double quotes, you avoid escaping issues. Try this:
echo '<p>'. $post['message']. '</p>';
i see that you didn't escaped from double quote that closes href attribute:
echo "<p><a href=\"https://www.facebook.com/rscmovement\" target=\"_blank\">"
I guess You have missed the back slash () before " after www.facebook.com/rscmovement.
"https://www.facebook.com/rscmovement\" "\"target=\"_blank\">" will

php var value contains quotes

So... I have a mysql_fetch_array and I'm running into an issue when some of the mysql data contains single or double quotes. This is the dumbed down version of my code:
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(\"" . $row['item'] . "\");'>" . $row['item'];
echo "</td></tr>";
}
The edit_form() function is used to send the value of the current item back to the value of the input in the form so the user can then easily edit their entry and then sends an UPDATE command to mysql along with the proper primary key id (which I left out because of irrelevancy). The only issue I have is if a user puts single or double quotes into the form then it messes up the onclick attribute. Please help!! I am pretty new to php and can't figure this out. I've messed around with htmlentites() and html_entity_decode() but am still getting no where. Thank you so much!
Use htmlspecialchars on $row['item'] before inserting it in your document.
So your "dumbed-down" code should be:
while($row=mysql_fetch_array($list)) {
$item = htmlspecialchars($row['item']);
echo "<tr>";
echo "<td onclick='edit_form(\"" . $item . "\");'>" . $item;
echo "</td></tr>";
}
Try the below line instead of the original in your code and see if it works:
echo "<td onclick='edit_form(\"" . str_replace('"',"&quote;",$row['item']) . "\");\">" . $row['item'];
And where you would like to display the $item field, just replace it in reverse if you get &quote; instead of ' " '. For example:
$qoute_free= str_replace('&quote','"',$passed_value);
If you are using it in javascript, the function can be as below:
function edit_form(passed_value)
{
new_value=passed_value.replace(/&quote;/g,'"');
}
I advise using json_encode. That way you don't have to worry about special cases htmlspecialchars might miss (such as newlines).
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(" . json_encode($row['item'], JSON_HEX_APOS) . ");'>" . $row['item'];
echo "</td></tr>";
}
I'm just putting the values back into my <form> (when they click on a <td>) and when they hit SUBMIT it directs the data to a different .php file that uses a MySQL UPDATE instead of an INSERT. I finally found some code that works!
$list is a mysql_query I ran at the top of the document
$item is the title of one of my columns in MySQL
while($row=mysql_fetch_array($list)){
$item = json_encode($row['item']);
$item = str_replace("'","'",$item);
echo "<td onclick='edit_form(" . $item . ");'>" . $row['item'] . "</td>";
}
Now it display's correctly in the <form> when <td> is clicked (by jQuery input.val($item)) and it shows up in the table correctly via $row['item']. I don't really understand exactly how it's working (how the encode is making it work) but I am glad it is. Crazyness!!!! Thanks for responding! and thanks for your effort!!

Categories