My onclick events in my php include file do not work - php

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.

Related

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>";

WP ACF echoes variable even if not wanted

I have strange problem, i've done the same thing before, many times.
But for some reason this one field just dosent want to co-operate whatsoever...
<?php if (have_rows('subfamilies_product_changes')) :
while ( have_rows('subfamilies_product_changes') ) : the_row();
$url = the_sub_field('subfamilies_product_changes_link');
echo "<div class='support-lahko'>";
echo "<div class='support-logo'>";
echo the_date( 'Y-m-d' );
echo "</div>";
echo "<div class='news-name-product'>";
echo "<h3 style='vertical-align: middle;'>" . the_sub_field('subfamilies_product_changes_title') ."</h3>";
echo "</div>";
echo "<div class='news-desc'>";
echo "</div>";
echo "</div>";
echo "<br>";
endwhile;
endif;
It seems, no matter where i put my <a> tags, this script is just going to go ahead and for some reason echo the link from $url = the_sub_field('subfamilies_product_changes_link'); into where ever it is defined. when i try to do something like <a href=' . $url . '> It will still just echo the url in <p> tags in seemingly random place.
Here is picture of what happens when i put $url inside href:
http://imgur.com/a/7AQsl
href stays empty, and wp just echoes url where it was defined. none of the text in image is a link.
I would just need to echo the url INTO the href, so i can iterate links into pdf files.
the_sub_field('subfamilies_product_changes_title')
produces text: Test Changes, as expected.
Short answer - use get_sub_field();
the_sub_field() outputs it immediately.
Glad that it helped - sometimes it is the smallest thing that ruins our day.

Syntax Error in concat echo statement and html code

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'] ."\">";

How to pass a query string value to a page loaded via AJAX?

I am trying to put a variable into an AJAX-loaded page link I can call it on the content it loads?
I have tried:
<a href="#" onclick="ajaxpage('/content/staff/profile.php?nav={$array['username']}', 'content')">
<?php
$i++;
echo "<div style=\"float:left; width:{$row_width}%;text-align:center;\">\n";
echo "<i>»{$array['username']}</i>\n";
echo "</div>\n";
echo "</a><br />\n";
I tried putting the link inside and outside the php code but neither of them worked, I also tried a few differents ways of calling it including $array['username'] All I need is a way I can define the variable "nav" as username (which is set in a database) so I can use it on that content that is called by the link.
Is it even possible?
I am not sure I completely understood your question.
But perhaps you want to try this out:
<a href="#" onclick="ajaxpage('/content/staff/profile.php?nav=<?php echo $array['username'];?>', 'content')">
<?php
$i++;
echo "<div style=\"float:left; width:{$row_width}%;text-align:center;\">\n";
echo "<i>»", $array['username'], "</i>\n";
echo "</div>\n";
echo "</a><br />\n";

using ' and " in php syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get useful error messages in PHP?
Ive started on part of my new year resolution and decided to learn php, as part of it im trying to parse in an xml feed, and echo out the name of the events wrapped in <a> tags linking them back to the events page on the xml feed's site.
I think ive got it all in but i cant seem to see why this isnt working im just getting a blank page, if some one could point me in the right direction it would be much appreciated, cheers
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href="$event_attributes['url']">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
</ul>
echo "<a href="$event_attributes['url']">";
try changing that line to
echo "<a href=\"".$event_attributes['url']."\">";
The Php parser is pretty funny about this. Usually you pick one and just stick to it, or use both single quotes and double quotes as you please. Just remember that strings with double quotes are parsed for variables.
$hello = "Hello";
echo "$hello master";
is the same as
$hello ="Hello";
echo $hello.' master';
When you are testing your PHP scripts, you'll find it useful to switch on errors - then PHP will actually tell you why it isn't showing you anything:
error_reporting(E_ALL);
Normally you will have missed a ; or mis-typed a variable name.
in your case the error is here:
echo "<a href="$event_attributes['url']">";
You have accidentally ended the string with a double quote, so PHP thinks the string ends here:
echo "<a href="
This is where using single-quotes can be very handy because your double quotes won't then close the string.
echo '<a href="' . $event_attributes['url'] . '">';
The main difference between single and double quotes in PHP is that double quotes has special clever parsing rules and single quotes doesn't. For example:
$myVar = "BLAH";
echo "Example $myVar"; // Example BLAH
echo 'Example $myVar'; // Example $myVar
In your unordered list, you should use a dot to concatenate your string, and escape your double quotes like this:
echo "<a href=\"".$event_attributes['url']."\">";
Instead of
echo "<a href="$event_attributes['url']">";
Your example throws and error because you haven't used proper string concatenation. However, even with correct concat, it would render as <a href=http://someurl>, and you'd need to add the double quotes according to html standard. Hence you have to double quote.
if you want to not be troubled by having to switch between using a ' or a " then i suggest using the php alternative syntax php alternative syntax
with the given code it would look like
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php foreach($type->market as $event):?>
<li>
<a href="<?php echo $event_attributes['url']; ?>">
<?php echo $event_attributes['name']; ?>
</a>
</li>
<? endforeach;?>
</ul>
one advantage this would bring is that it would produce cleaner code since you can clearly distiguish your php code from your html which is the presentational part at the price writing all those other <?php ?> and as what others would claim a performance degradation. the choice is yours
Change
echo "<a href="$event_attributes['url']">";
for
echo "<a href=".$event_attributes['url'].">";
You are missing the periods in your second echo, where you have your $event_attributes['url']
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href=".$event_attributes['url'].">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
I would recommend you to enable your error log, it would allow you to know the line with problems in any of your scripts.

Categories