This question already has answers here:
How can I convert language of a div?
(4 answers)
Closed 7 years ago.
"<div id="diva">this is div a</div>"
"<div id="divb"> this is div b</div>"
now how can I change the text of div dynamically by button click. here also mention that all text come from a database. when I click the button then it detects the div id and replace the text with desire text.
All code will be in PHP.
This will work once the page is being loaded:
document.getElementById('diva').innerHTML =
<?php echo functionThatReturnsSomeValue(); ?>;
Dynamically, you would need to do an AJAX call on the PHP file which outputs a value. You'd be better off if you used a JS framework such as jQuery, rather than implementing the AJAX call yourself.
PHP cannot modify the content of the page after it has been served to the browser. However this would be trivial with a JavaScript Library such as jQuery.
Related
This question already has answers here:
Events triggered by dynamically generated element are not captured by event handler
(5 answers)
Closed 8 years ago.
page-one.php contains such jquery
$.post("___for_ajax_process.php", data_to_send_ajax_post, function(data_edit_content) {
$('#show_result').html(data_edit_content);
});
in #show_result i see content from ___for_ajax_process.php
Rendered content contains many checkboxes and want to create check all. check all is named draft_check_all.
At first want to check what happens if I click the checkbox.
$("#draft_check_all").click(function(){
alert ( 'click test' );
});
And I do not see alert ( 'click test' ); No popup at all.
Then View source and I do not see source code for content rendered by ___for_ajax_process.php
As understand in such way can not apply jquery to content from ___for_ajax_process.php? What if place $("#draft_check_all").click(function(){ in ___for_ajax_process.php? Are there any solutions for such situation?
Solution
placed jquery in external php and all works!
A better solution would be event delegation.
$('#show_result').on('click', '#draft_check_all', function () {
...
});
This question already has answers here:
How do I redirect to another webpage?
(58 answers)
Closed 9 years ago.
Can I use a header location in jQuery for redirection or refresh?
Like in PHP:
header('location:www.google.co.in');
header("Refresh:1,url=home.php");
If not, what is the alternative way?
Headers are interpreted prior to the rendering of the document, and before jQuery is even loaded so these aren't an option. Instead, you can redirect the browser using document.location.
document.location.href = 'www.google.co.in';
For a jQuery approach you can use
$(location).attr('href', 'www.google.co.in');
however I would favor the plain javascript version.
You can use:
window.location.assign('www.google.co.in');
or
window.location.href = 'www.google.co.in';
or
window.location.replace('www.google.co.in');
The difference is that assign() will just cause a new document to load. While replace() will replace the current document and replace the current history with that URL making it so you can't go back to the previous document loaded.
Use
window.location.href="www.google.co.in";
for redirection.
and
window.location.reload(true);
to reload the page
This question already has answers here:
jQuery copying content between DIVs
(4 answers)
Closed 9 years ago.
I do need to know how to copy the contents of an entire html td td and play for another from a different button that was clicked?
both the button when the content comes from the database
was thinking about this for jquery but not know where to start: (
This should go with:
$('td').clone().appendTo('.newtd');
you trigger that with eg following:
$('#triggerbtn').click(function() {
$('td').clone().appendTo('.newtd');
}
markup for the button:
<div id="triggerbtn">Click me for clone</div>
the .newtd is the class of the new td you want to clone your content. you can use an id declared with a #newtd as well.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing variables from php to javascript
I am using this awesome jQuery Stick Notification Plugin which creates notifications upon clicking various buttons.
I used the following code to display the notification box...
$(".notify").click(function() {
$.sticky('The page has loaded!');
});
Can i make it display some PHP variables in place of static text message?
Hope i've made it clear enough.
Without making asynchronous HTTP calls, you will have to insert the PHP variable at server-side:
$(".notify").click(function() {
$.sticky('<?php echo htmlentities($message); ?>');
});
Wrap it with htmlentities() just in case $message contains some chars that make the JavaScript string invalid.
yes you can like
$(".notify").click(function() {
$.sticky('<?php echo "The page has been loaded"; ?>');
});
Good day. I'm trying to make an online quiz application using php. The idea is that the form will add a text field for the question and four other text fields below it for the answers each with a radio button to determine which one is correct. Then below the form is an add a new question button which would call an ajax post function. The problem is instead of appending to the target div, the php code replaces the contents of the div itself. Is there a way for php to append to the div instead of replace its contents?
PHP is server-side, it can't do anything in the browser, only deliver content.
To add a div, you use Javascript in the page, e.g.
var el = document.getElementById('idoftarget');
el.innerHTML += '<div>new stuff</div>
... I think. I always use jQuery:
$('#idoftarget').append('<div>new stuff</div>');
how are you handling the ajax response? maybe instead (since you don't have code here it's a guess) of
document.getElementById('someDiv').innerHTML=ajaxResponseText;
you want
document.getElementById('someDiv').innerHTML+=ajaxResponseText;
(appending rather than overwriting)
This is the right one:
document.getElementById('exampleDIV').innerHTML+=ajaxResponseText;