I am having problems updating a SPAN in my HTML with the result from a PHP file. In the HTML below the is a link that calls the php file that will update the SPAN above it with the new value.
HTML
<div class="link-votes">
<span id="v<?php echo $link_id; ?>"><?php echo $votes; ?></span>
</div>
PHP
if($_POST['id']) {
$up_value=$row['vote_count'];
$("#v"+id).html(html) = $up_value;
}
If I echo $up_value the right integer is printed. But I cannot update the value in the SPAN.\
Thanks,
You are mixing Javascript code with PHP code. You are using the html() function which looks like it might be from the JQuery library. You might want to echo() in PHP and then use Javascript to work on the return value of that echo() statement. It should work as the echo() will be done before the Javascript takes effect, as is the required flow of execution.
Thanks all, your advice helped me figure it out. What I did for other newbies reference was
echo $up_value;
at the end of the php file and then in my jquery:
success: function(html) {$("#v"+id).html(html);}
as the comments to your question show, you're mixing php & jquery code.
your php code should look something like this:
if(isset($_POST['id'])) {
$up_value=$row['vote_count'];
echo "<script>$('#v".id."').html('".$up_value."');</script>";
}
Related
I have content that I want to display in a textarea. And on a another page in a div.
The content I got from inside my database. And was inserted in the database with CKeditor.
Sound easy, right?
The problem is that when I use echo or print I am getting <b>Some content</b> instead of Some content.
Believe it or not I spend 6 hours already trying to solve this problem. What is normal amount for me to spend on a problem. Only normally I am at least one step closer solving the problem. But now I am still as clueless as I started.
And yes I am using: <meta charset="utf-8">
My code(Just a basic echo, since I don`t know what to do)
<textarea ><?php echo $content; ?></textarea>
Current output:
<li><s><em><strong><span class="marker">Dit is gewijzigd?</span></strong>
But I would like this:
Dit is gewijzigd?
You may try it like this to make the content editable
<div contenteditable><?php echo $content; ?></div>
EDIT :
try this
<div><?php echo htmlspecialchars_decode($content); ?></div>
Just use strip_tags() function :
<textarea ><?php echo strip_tags($content); ?></textarea>
Docs here : http://php.net/manual/fr/function.strip-tags.php
you can do couple of things to figure out
1) use the html_entity_decode function which will decode the html entity which is coming from database and return the html output
2) if the above solution doesn't work then use the first try to echo with html_entity_decode to check that does it work properly or not if it does then there is something we need to do with editor as so that editor will know that it is getting the raw html data not just plain text. i also used the FCKeditor where i have simply put the text and its working fine.
You can do this using js as -- You need two times decoding since as you shown your raw data in db ..
<textarea id='t'></textarea>
<script>
function getHTML(code)
{
var para = document.createElement("DIV");
para.innerHTML=code;
//return $(para).text();
return para.innerText;
}
document.getElementById('t').innerHTML=getHTML('<?php echo $content; ?>');
</script>
PURE PHP
<textarea id='t'><?php echo html_entity_decode(html_entity_decode($content));?></textarea>
Why does this not work? This is the first thing in the body:
<?php
if(isset($_POST['submit'])){
echo "<script>$('.classToShow').show();</script>";
}else{
echo "<script>$('.classToShow').show();</script>";
}
?>
classToShow is a simple div in the body. It won't show up and its not depending on the boolean condition, it must be the code...
While this works:
<?php
if(isset($_POST['submit'])){
echo "<script>alert('works');</script>";
}else{
echo "<script>alert('works');</script>";
}
?>
So the simple JavaScript works, but the jQuery doesn't... Why is this?
This is your problem:
This is the first thing in the body
At that point the element with the class of classToShow does not exist yet, so nothing happens. You should wait for the DOM to be ready before you run that code.
On the other hand, if you just want to show something when a POST request was made, you can add it directly using php and you don't need jQuery to do that afterwards.
A common solution would be to show it directly using php and then use javascript to hide the message after a certain timeout.
You can use $(document).ready() and inside that write the code
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.
When i embed or use php loops to create multiple div's with some id.
I want each of them to use with jquery. Say anything like simple hide/show interface.
But as the id is the same it wont work. I assign a variable lets say $i to php loop for the reference of uniqueness. I can assign id to a DOM element with like
<div id="element-<php echo $i ?>">Bla Bla </div>
Php echoes out here perfectly but how do i use it with jquery something like this:
$("#element"+<?php echo $i ?>).toggle();
Here it doesnt works.
How do i do it correctly and where am i going wrong ?
Sorry for bad english.
Thanks
You can try this
<script>
function togl(id)
{
$("#element"+id).toggle();
}
</script>
<?php
for ($i=0;$<10;$i++)
echo '<div id="element-'.$i.'" onclick="toggle('.$i.')">Bla Bla </div>';
?>
the proper syntax for a php code is:
<?php
//code
?>
So here you can clearly see what is the error:
<php echo $i ?>
It should be:
<?php echo $i; ?>
Updated answer:
<?php
for ($k=0;$k<=100;$k++)
echo '<div id="element-'.$k.'" onclick="toggleID('.$k.')">Here is the content of div $k</div>';
?>
<script>
function toggleID(id)
{
$("#element"+id).toggle();
}
</script>
You didn't include the dash in the jQuery code and you also don't need to concatenate the strings:
$("#element-<?php echo $i; ?>").toggle();
PHP is interpreted on the server and JavaScript is interpreted on the client. You should never consider concatenating strings between these environments since they are two different steps in serving your application.
I think your code for the javascript should be like this instead of what you posted.
$("#element-"+<?php echo $i ?>).toggle();
However I wouldn't recommend this approach as it's not very efficiant. It will trigger a lot of jQuery lookups.
You'd be better off giving all the divs in question a class and then using the class to reference them in jQuery.
<div class="toggleThis" id="element-<php echo $i ?>">Bla Bla </div>
$(".toggleThis").toggle();
Your problem is that you didnt' close your php statement:
You're missing the ";?>"
try this:
;?>
ath the end of your statement
EDITED:
plus you're missing the "-" between "element" and "id":
Ex. element-1
try including the "minus":
$("#element-"+id)
The problem is that you cannot have the same ID's. But if you just want simple show/hide functionality, you should add a class to your div instead of an ID. So..
class="toggableDiv"
and then in jQuery
$(".toggableDiv").click();
Hi i am not a php developer, ive never touched it before. but i have been asked to add a google shopping cart tracking code to a website. when someone completes an order then get sent to finishorder.php. when i go the finishorder.php file it looks like this:
include(dirname(__FILE__)."/init.php");
$GLOBALS['ISC_CLASS_ORDER'] = GetClass('ISC_ORDER');
$GLOBALS['ISC_CLASS_ORDER']->HandlePage();
which just looks like server script to me (coming from a .net background), so i presume i cannot add the javascript here, how does php decide get the layout for this page? how can i add the javascript code to this page.
You can do this:
include(dirname(__FILE__)."/init.php");
$GLOBALS['ISC_CLASS_ORDER'] = GetClass('ISC_ORDER');
$GLOBALS['ISC_CLASS_ORDER']->HandlePage();
echo '<script type="text/javascript">YOUR JS HERE</script>';
OR
<?php
include(dirname(__FILE__)."/init.php");
$GLOBALS['ISC_CLASS_ORDER'] = GetClass('ISC_ORDER');
$GLOBALS['ISC_CLASS_ORDER']->HandlePage();
?>
<script type="text/javascript">YOUR JS HERE</script>
Hmm?
But I think that HandlePage() method will do something with our page so I'd look inside this method Class ISC_ORDER->handlePage() what it does... You can then echo Your within this method on appropriate place...
EDIT:
<?php
echo '<script type="text/javascript">//<!--
alert("Hello to multiline JS script");
alert("Do You get it?");
//--></script>';
?>
You can add javascript inside a php code as
<?php echo "<script> alert('this is a javascript code')</script>"; ?>
You can add script in PHP page by 2 ways
The first way is to add it in PHP tags
<?php
//PHP CODE
if($_POST['submit']){
echo '<script>alert('Hello')</script>';
}
?>
The second way is to add it after PHP code
<?php
//PHP CODE
?>
<script>
alert('Hello');
</script>