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.
Related
I'm trying to call a javascript function within php that will pop up a confirmation button. If the user presses yes, then it will proceed onto the page, otherwise it'll stay on the same page. I wrote it, but I have no idea what's wrong.
php:
echo "Delete<br/><br/>";
javascript (i placed it right before the tag):
<script type="text/javascript">
function deleteMembers(url, id) {
var deleteMemberConfirmation = confirm("Are you sure you want to delete?");
if(deleteMemberConfirmation) {
window.location="http://mvcsf.com/admin/"+url+"?"+id;
}
else {
window.location="http://mvcsf.com/admin/view_members.php";
}
}
</script>
I enabled ERROR_REPORTING(E_ALL); at the top of the page, but it's not returning anything. What did I do wrong?
Edit: I changed the variable names to deleteMemberConfirmation, but still nothing works. I just click the link, but nothing happens.
delete is a reserved keyword in javascript, and not a valid variable name!
And you got the quotes wrong:
"<a href=\"javascript:deleteMembers('del_member.php', '$studentid');\">";
You're using ' as a designator in your HTML AND in your JS. You will have to use it in one place and " in others.
A working version would be something like:
echo "Edit or Delete<br/><br/>";
For your echo, be careful when using single quote ' and double quote ". A single quote will be closed when it meets another single quote unless it is escaped like this \'. The same goes for double quote.
I'm not 100% sure if you can use javascript inside href, but another solution is to use onclick when calling javascript function, and just use javascript:void(0) or # for href attribute.
echo "Delete<br/><br/>";
As for the delete, change the delete word to something else (I.e: del), because delete is a reserved word for javascript.
<script type="text/javascript">
function deleteMembers(url, id) {
var del = confirm("Are you sure you want to delete?");
if(del) {
window.location="http://mvcsf.com/admin/"+url+"?"+id;
}
else {
window.location="http://mvcsf.com/admin/view_members.php";
}
}
</script>
I have a registration form that I'm working on and it's turning out to be a pain.
I'm very new to PHP, so please cut me some slack - haha.
I installed a jQuery plugin that allowed me to make inline labels for my textboxes. I also created an error box for any errors that occur during the registration process (invalid email, etc.). Here's some of my HTML/PHP code.
<?php
if($_POST['submit'])
{
$signuperror = "Hello World";
?>
<?php if($signuperror != "") { ?>
<span id="signuperror"><?= $signuperror; ?></span>
<?php } ?>
The problem was that the "error" of "Hello World" was not displaying when I clicked the submit button on my form. I copied and pasted this code onto a test.php document and it worked fine. So I knew that it had to be from my other html code. After troubleshooting almost every line of code, I found the culprit. It turns out that the jQuery plugin initialization for the inline labels was the problem.
$(function(){
$.fn.formLabels();
$("form").submit(function(){
var formVal = $("form").serialize();
parent.$("#default div.results").html(formVal);
return false
})
});
When I deleted this, it worked just fine (without my inline labels, of course).
What could I do to make BOTH the PHP and jQuery work.
Thanks.
- Ryan
Notice the return false at the end of the $("form").submit() function. That means the jQuery function is taking the place of your form's POST action. You're not reloading the page synchronously, so you don't have any value for $_POST["submit"]. Get rid of the return false line, and see if the page reloads as you're expecting.
There are lot of things here that are going on. Need to do this step by step.
Your PHP should either use <?php format or use <? short form but to be sure i would code all in <?php so its compatible everywhere
When you need to echo or output something use <?php echo $variable; ?> rather than <?=. Not that its no good or so but it will take out any php config issues with asp style output.
First test php output then check if statement.
Jquery is not messing with your PHP. That title is just as random as my answer.
So, in an html page I'm trying to have a php segment echo some javascript code, as seen here:
<?php
echo "This was legitimately hit";
if(!empty($_POST['name']))
{
echo '<script type="text/javascript">alert("We got the name");</script>';
}
else
{
echo '<script type="text/javascript">alert("We DID NOT get the name");</script>';
}
?>
and from what I've read online, this seems to be a legitimate way of doing things, but the page seems to be reading the first part up until the first closing chevron (seen just below here) as a comment.
<?php
echo "This was legitimately hit";
if(!empty($_POST['name']))
{
echo '<script type="text/javascript">
Then it reads the else and next echo as plain text, and puts it on the webpage. the next javascript code block then gets read as a regular javascript code block, so the page does a pop-up saying it did not get the name. The closing bracket and closing chevron then just get output as more text.
So in the end the page just ends up having
alert("We got the name")'; } else { echo ''; } ?>
printed on it as plain text, and has a pop-up that says we received no name.
What is going wrong here?
Sounds like the file isn't being processed as PHP. Does the file name end in .php? Are you sure PHP is installed and hooked up correctly to the web server?
edit: To handle the Facebook requests in the same page:
<?php
if (isset($_POST['facebook_request_field'])) {
// handle the Facebook request, output any necessary response
// then exit
exit;
}
?>
<!-- display the web page normally here -->
So for your test page:
<?php
if (isset($_POST['name'])) {
echo '<script type="text/javascript">alert("got a name!");</script>';
exit;
}
?>
<script type="text/javascript">alert("No name.");</script>
(That's actually identical in function to what you already have, so maybe I'm misunderstanding the purpose.)
Between We got the signed request and We got the name, I think you haven't given us the actual code that's causing the error. Double check that, and make sure you don't have any stray single quotes before your alert call.
There are missing ; after the alert. Have you tried correcting this first?
I have a variable in which a complete html is saved.
$body= $myhtmlpage;
<a onclick="openWin(' <?php echo htmlspecialchars(json_encode($body)) ?>');" href="javascript:void(0);"> Click </a>
and i have this javascript function which display the text in new window.
<script type="text/javascript">
function openWin( str )
{
myWindow=window.open('','','width=400,height=400');
myWindow.document.write(str+"<p>This is 'myWindow'</p>");
myWindow.focus();
}
</script>
When there is simple text in my body, it works fine. but if there is some html then it does not display, I am new to javascript. please tell me how can i prepare my HTML that it should be passed to Javascript html. i tried htmlspecialchars(json_encode($body))
functions but still having problem.
Uncaught SyntaxError: Unexpected identifier
You will have a long battle trying to get a lot of HTML to work as a string variable in Javascript. It would be far better for you to put that markup in a hidden block (like a DIV) in your markup and then just get the contents of that markup and show it in your window.
This has the added advantage of allowing your hidden markup to be validated. It is very hard to debug a lot of html markup stuffed into a string variable, but when included in the DOM as real markup it makes your life much easier.
UPDATE: Adding some sample code:
<div id="my_hidden_content" style="display:none;">
<?php echo $body; ?>
</div>
<a onclick="openWin('my_hidden_content');" href="javascript:void(0);"> Click </a>
Now the javascript:
function openWin( contentId )
{
var contentContainer = document.getElementById(contentId);
var content = contentContainer.innerHTML;
myWindow=window.open('','','width=400,height=400');
myWindow.document.write(content+"<p>This is 'myWindow'</p>");
myWindow.focus();
}
Firstly, you do not need to use json_encode(), that is just confusing the situation.
Secondly, the problem will be that your HTML contains quotes. This will result in a syntax error in the HTML you output, since htmlspecialchars() does not escape quotes.
Use htmlentities() with the ENT_QUOTES flag instead. So change the line to this:
<a onclick="openWin('<?php echo htmlentities($body, ENT_QUOTES) ?>');" href="javascript:void(0);">Click</a>
Thirdly (although it should probably be firstly since it is the most important point) your approach to this is all wrong. If you're opening a new window, you should have it load a page from the server and generate the HTML when the window is opened.
You can try html code inside php code, instead php code inside html.
for e.g.
<?php
echo "<a onclick='openWin(\" ".htmlspecialchars(json_encode($body))." \")'>Click</a>";
?>
That's because, HTML isn't JSON. For that simply use: htmlspecialchars($body)
I have been trying to get this to work for a while, and I suspect there's an easy solution that I just can't find. My head feels like jelly and I would really appreciate any help.
My main page.php makes a .post() to backend.php and fetches a list of cities which it echoes in the form of:
<li onclick="script('$data');">$data</li>
The list is fetched and put onto the page via .html().
My problem occurs when $data has a single quote in it. backend.php passes the variable just fine to page.php but when i run html() it throws a javascript error (in IE, not FF obviously);
')' is expected
IE parses the single quote and messes up the script()-call. I've been trying to rebuild the echoed string in different ways, escaping the 's on the php side and/or on the javascript side - but in vain. Do I have to review the script in total?
page.php
$.post("backend.php", {q: ""+str+""}, function(data) {
if(data.length >0) {
$('#results').html(data);
}
backend.php
while ($row = $q->fetch()) {
$city = $row['City'];
// $city = addslashes($row['City']);
// $city = str_replace("'","'",$row['City']);
echo "<li onclick=\"script('$city');\">".$city."</li>";
}
You need two encodings: One for the JavaScript context and one for the HTML context:
json_encode is for encoding the data for JavaScript and
htmlspecialchars for the HTML part.
So try this:
echo '<li onclick="script(' . htmlspecialchars(json_encode($row['City'])) . ')">' . htmlspecialchars($row['City']) . '</li>';
You could call script() and then reference this.innerhtml.
<li onclick="script();">$data</li>
And then in your javascript:
function script() {
data = this.innerHTML;
// do stuff
}
what about backslash escaping it?:
$city = str_replace("'","\'",$row['City']);
To make your life easier, I would dispense with using onclick if you can and include scripts in the <head> of the page and register click handlers based on id/class. And even better, use jQuery to do that, but you're probably aware of jQuery already if you've been around S.O. enough.
Single/Double/Triple escaping all the time just isn't worth it.
My contribution to proceedings, uses your existing function with minimal change.
<li onclick="script(this.innerHTML);">$data</li>