I created a jquery function in a js file that is included in a php page.
Js file
$( function() {
function disab(){
$('input#idname').attr('disabled', 'disabled');
}
});
In my php file I tried to call the disab function in this way but with no success.
echo "<script>";
echo "$(function(){";
echo "disab();";
echo "});";
echo "</script>";
How can I do this? thanks
first of all put your funciton outside the document.ready function in your js file...so that it is global and can be accessed from anywhere.
function disab(){
$('input#idname').attr('disabled', 'disabled');
}
$( function() { .. }); //use if needed..
and call the function inside <script> tag in php file
<?php
//all you php related codes
...
?>
<script>
$(function(){
disab();
});
</script>
and most important, use prop() instead of attr() ... if you are using latest version of jquery (jquery 1.6+ )
$('input#idname').prop('disabled', true);
Related
The following function is inside a page (mainpage.php) which I load dynamically into a tab (tabcontent.php) .
$('.editlink').on('click', function() {
var id = $(this).attr('data-ds');
$.ajax({
url: 'noticejob.php?step=get&ds='+id,
method: 'GET'
}).success(function(response,status) {
...
});
});
I call this function by this link, which is inside the tabcontent.php
<i class="fa fa-pencil"></i>
All works fine.
Now I want to start this function over a url call for adding links into other pages to open the mainpage.php and start with this function (should open a modal).
For Example: mainpage.php?start=edit&ds=123
Is it possible and in which way?
You can just print the JS you need to execute in the PHP page.
...
?>
<script>
$(function() {
foo();
});
</script>
<?php
...
To open the modal automatically, either create a JS function that opens the modal and call it (with the previous method), or do
<script>
$(function() { $('.editlink').click(); });
</script>
Ok, now it works,
i've put a new (named "edit_function") js function into the mainpage.php with the "old" code of the "$('.editlink').on('click', function() {..."
after that i call this function from the mainpage if there is a url parameter. I do it into the php file and create a dynamicaly js code
if($_GET['job']=='new')
{
$p['JS']='edit_function("'.$_GET['detail'].'");';
}
at the other hand i call the function for the tabcontent.php page in this way
$('.editlink').on('click', function() {
var id = $(this).attr('data-ds');
edit_function(id);
});
for me it works
I am uploading files using a hidden iframe as a target...from inside this iframe I want to run a function that's on my main page.
In my target iframe.php I have
<script>
upload_completed(<?php echo $numerolinha ?>);
</script>
but I get from firebug:
ReferenceError: loading_linha_21 is not defined
parent.upload_completed();</script>
If I run upload_completed(loading_linha_21); on my main page it runs ok so I'm guessing the command to run the function on the parent page isn't working... help!
I also tried :
window.top.upload_completed(<?php echo $numerolinha ?>);
window.upload_completed(<?php echo $numerolinha ?>);
my functions :
<script >
function upload_started(id){
$(id).css("display","block");
}
function upload_completed(id){
$(id).css("display","none");
}
</script>
The best guess i can give you is that the jQuery function requires the id to be #name_of_id, so when you call the function from the iframe like so:
parent.upload_completed('#<?php echo $numerolinha ?>');
or you can change your function to do this:
function upload_started(id){
$( '#' + id).css("display","block");
}
function upload_completed( id ) {
$( '#' + id ).css("display","none");
}
Put a ' inside function like below :
<script>
upload_completed('<?php echo $numerolinha ?>');
</script>
I am just starting with PHP. What i am trying to get to do is change the color of a div from a function that's in an included page. I was able to do this in jquery using load() function.
Here is my current work:
index.php:
include 'functioncontainer.php';
<div style="background-color:#ffff00;">I want this back. color changed</div>
functioncontainer.php:
`<script>
$(document).load(function() {
$('.change').click(function() {
$('div').css('background-color','#06C');
});
});
</script>`
I don't know php but I think you need to echo
echo "<script>
$(document).load(function() {
$('.change').click(function() {
$('div').css('background-color','#06C');
});
});
</script>";
I have an ajax function that post to an PHP file.
Now since I'm using WordPress I can use the get_url function so I don't need to hard code the entire URL.
The WordPress function is an PHP so I'm trying to use PHP inside the ajax post. But it wont do the trick.
Any ideas ? and is it possible ?
This is what I have.
$(document).ready(function(){
$('#submit').click(function(){
$.post('<?php echo get_template_directory_uri(); ?>/send.php', $("#mycontactform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
});
I have also tried the php echo inside quotes like this.
$.post(' "<?php echo get_template_directory_uri(); ?>" /send.php' ....
ether way I get this path
http://mysite.com/%27%3C?php%20echo%20get_template_directory_uri();%20?%3E%27/send.php&email=&message=&name=&sent=1
Javascript to PHP = nope you cant embed javascript to php, only php can embed html and javascripts.
the better way to do it is to create a .php file and insert the javascript there...
Example: js.php
<?php
function doSubmit() {
?>
<script>
$('#submit').click(function(){
$.post('<?php echo get_template_directory_uri(); ?>/send.php', $("#mycontactform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
</script>
<?php
}
?>
call the js.php using "include(js.php);" and call the functions inside another php
Inside your index.php
<?php
include('js.php');
?>
<html>
<head><script><?php doSubmit();?></script></head>
<body>
</body>
</html>
With this javascript I'm printing list of records:
<script type="text/javascript">
$(document).ready(function(){
function hide(){
$('#friend_list').fadeIn(100);
$('#friend_list').load("friend_list.php");
};
setInterval( hide,100 );
});
</script>
in friend_list.php I'm getting all the records relevant for user and returning it like this:
echo "<div class='friend' id='friend'>" . $a["username"] . "<input type='hidden' value='$id'/>" . "</div>";
And I'm using this simple script to make overlay:
<script type="text/javascript">
$(document).ready(function(){
function overlayerOn(){
$('#overlayer').fadeIn(800);
}
function overlayerOff(){
$('#overlayer').fadeOut(800);
};
$('#board').click(function(e){e.stopPropagation();});
$('.friend').click(function(e){
e.preventDefault();
overlayerOn();
var $br = $('#board');
$br.css('display', 'block');
});
$('#overlayer').click(function(e){
overlayerOff();});
});
</script>
Overlay is working as long as I trigger it with some other id or class than the one used from friend_list.php. But that is the one I need. Any idea why overlay is not working when triggered by class .friend?
Thank you...
Use (live is deprecated)
$('.friend').live('click', function(e){
// your code
});
If you are using latest version of jquery then use
$('body').on('click', 'div.friend' , function(e){
// your code
});
It's happening because you are loading content dynamically using
$('#friend_list').load("friend_list.php");
so click is not working because those contents were not in the DOM when it was loaded.