How to put html href button inside php code - php

I'm trying to put an html href button inside of my php code, but when I run it, I get an error message saying "This Page Isn't Working."
<?php
echo "<button onclick="location.href='phpfile.php';">My Button</button>";
?>

Either you need to escape the HTML double quotes:
echo "<button onclick=\"location.href='phpfile.php';\">My Button</button>";
Or the Javascript single quotes:
echo '<button onclick="location.href=\'phpfile.php\';">My Button</button>';
By the way,
This Page Isn't Working.
Means a 500 error, which means your php script does not compile. Most of the time it is due to syntax errors.

Related

Pass a PHP variable in a link to a modal window

Hi I have a php link which calls a modal popup window which all works fine, the problem i am having is that it doesn't pass the php variable in the link.
the php variable shows in the address bar, but it isn't passing to the modal form.
here is my link
echo "<a href=\"?ip_address='.$ip_address.'#accSettings1\" >Add</a>";
And I am getting the variable like this.
$ip_address = $_GET['ip_address'];
echo "$ip_address";
You need double quotes and not single quotes
<? echo "<a href=\"?ip_address=".$ip_address."#accSettings1\" >Add</a>"; ?>
-^-------------^-
It will pass . and ' as literal string, the output will be something like
Assuming $ip_address = 'hello';
<a href="?ip_address='.hello.'#accSettings1" >Add</a> <!-- HTML Source -->
-^-------^-
This is why the link breaks
so it is adding unnecessary periods and single quotes in the link which breaks it
And the code I provided will be
<a href="?ip_address=hello#accSettings1" >Add</a>

PHP - Call echo script outside

I've got a real problem, I want to make an alert using PHP to call JavaScript, but my PHP is inside a textarea, is there anyway to echo out the script outside the textarea without moving the PHP code?
<textarea>
<?php
echo "<script language='javascript'>alert('Hello!')</script>"
?>
</textarea>
No, not really. Once you're inside a <textarea>, ANY text, including html tags, will be considered part of the textarea's "to be edited text", up until the first </textarea>. You will have to output your script tag outside of the textarea, which means moving your PHP code.
<?php
echo "<script type="text/javascript">alert('Hello!');</script>"
?>
<textarea></textarea>
This function and the alert you can't write inside the textarea

avoid alert Refused to execute a JavaScript script. Source code of script found within request

How I can prevent chrome throws error: Refused to execute a JavaScript script. Source code of script found Within request.
This occurs because my html code contains objects that launch one function when mousedown occurs,for example:
<div>
<?php
echo "<span onmousedown=\"myfunction();\"> example </span>";
echo "<span onmousedown='myfunction();'> example 2 </span>";
?>
</div>
The error occurs when redirected to the same page, however not the case when I first entered on the page or when it reloaded the page.
I do not want to disable the X-XSS-Protection ,I need a more elegant solution. A solution with code: php, html or javascript
I try to make a legitimate use of the function, not trying to inject script
try
<div>
<?php
echo "<span onmousedown='myfunction();'> example </span>"
?>
</div>
Notice single quote arriund myfunction()

popup window not work in php echo

it can appear a simple question but i have searched untill writing here but no answer. i have a php code and i what to start a pop up window after echo :
echo "<A HREF='map2.php' onClick='return popup(this,'notes')'>WHATEVER</A>";
in the head section i have :
<SCRIPT TYPE="text/javascript">
<!--
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=400,height=235,scrollbars=yes');
return false;
}
at the end is the ending script tag but i dont succide in adding it.
anyway.the pop up doesn work. the link opens in the same page.
i also tried :
About
and it doesnt work. it opens in the same page. The funny thing is that all these 2 solutions worked in html page, but when used between php , after "echo" , it doesnt work anymore.
In the first line you posted (the php echo), it seems to me you have a problem with ' in side '
Try the following:
echo "WHATEVER";
The issue here your quoting.
When outputting HTML I recommend using single quotes with echo as it allows you to use the proper double quotes for the HTML tags.
echo 'Whatever';
The problem with your original code was that you had quotes within quotes that were breaking the syntax. Read the link I posted to see how to handle quotes properly with PHP.

Javascript in PHP code

I found this code on the internet on how to to display message/pop up box.
<? echo "<script language=\"JavaScript\">\n";
echo "alert('$msg1')";
echo "alert('$msg2')";
</script>";
?>
AND
<? echo "<script>alert('$msg1' )</script>" <?
I want to display messages to the user by popup message. all the messages will be appears in one message box. For above example, the message will be appeared in two box.
Can it be done in all in one box? I try using '\n' or 'br>'...also cannot or i did it wrong? Any idea? Is there any reference or tutorial on this?
<?
echo "<script type=\"text/javascript\">\n";
echo "alert('$msg1" . '\n' . "$msg2');";
echo "</script>";
?>
EDIT: But your users may find alert annoying. Look into DIV-based dialogs.
"alert('$msg1" and "$msg2');" use double quotes to allow for variable interpolation. '\n' is single-quoted so backslash will not be an escape (we want it to be interpreted by JS, not PHP). . is PHP's concatenation operator.
There's a few other issues here.
It is using Javascript alert boxes, which are ugly, and modal. It's bad for users. Modal in the whole-of-browser sense, so (depending on the browser) users can't even go and do something in another tab while this message is on screen; they must dismiss it first. It's much better to place the message in a well styled <div> for example. You could still use some unobtrusive script (like jQuery) to allow users to hide the box if you were so inclined.
Any apostrophes in $msg1 and $msg2 won't be escaped in the Javascript output. This can be a security problem if you're accepting user input as part of these variables. You could use addslashes() to partially fix this, but you'd also need to escape the characters "</" (or "</script" if using HTML) if they might appear, and possibly other variants too.
If you read and accept the above problems and still want to achieve this, here's a safer (though I'm still not sure if it's perfectly safe) alternative:
<?
echo "<script type=\"text/javascript\">";
echo "alert('" . str_replace("</", "<'+'/", addslashes($msg1).'\n'.addslashes($msg2)) . "');";
echo "</script>";
?>
Instead of:
echo "alert('$msg1')";
echo "alert('$msg2')";
Try:
echo "alert('$msg1, $msg2')";
Here is modified example from w3schools.com tutorial if you want to display messages before submit is processed:
<html>
<head>
<script type="text/javascript">
function disp_alert()
{
<?php echo "alert('".$msg1.'\n'.$msg2."');"; ?>
}
</script>
</head>
<body>
<input type="button" onclick="disp_alert()" value="Display alert box" />
</body>
</html>

Categories