im curious, if the value of <p class="myclass"></p> assign by jquery using $('.myclass').text('txtvalue'); can be fetch using PHP script? example. like i want to fetch the assign value of jquery without using jquery post, ajax to pass value in PHP because im do this with only one page.
here my code:
<p class='myclass'></p>
<?php
$a = strip_tags("<p>","<p class='myclass'></p>");
echo $a;
// the result is 100; but its dismase like im printing "<p class='myclass'></p>"; i im trying to do is to get exactly the value of "<p class='myclass'></p>". because the value of that, i will use to query in mysql id.
//it is impossible to me to do this?...
?>
<script>
$('.myclass').text('100');
</script>
No, this cannot be done.
PHP runs server-side and generates your HTML, including any Javascripts with jquery in it. When it's done, Javascript gets to work, client-side. The only way to get something back into PHP is to send it to the server by posting it, ajax, or redirection.
Related
i am using thie jquery code to poll my database:
jQuery(function($){
setInterval(function(){
$.get( 'getrows.php', function(newRowCount){
$('#rowcounter').html( newRowCount );
});
},5000); // 5000ms == 5 seconds
});
and display the results here:
<p>There are <span id='rowcounter'>xx</span> rows in the DB.</p>
but i am not sure what to put in the getrows.php file... does there need to be any html tags with an ID of rowcounter or newRowCount?
After getting the number of rows in your database, just do:
<?php echo $rowCount; //Or whatever value you want to show ?>
That's all
Looking at that code, all the PHP file needs to do is emit a number. You'd have normal data-access code like you would anywhere else, then just:
echo $someValue;
jQuery intelligently guesses the type of data returned by your php script (definable in your php request's script with ContentType).
A simple php script would return HTML/Text data, so you just have to "echo" your number in your PHP script. Then your newRowCount JS value will contain your PHP-echoed number.
You're simply requesting getrows.php and expecting a response from it.
You can do anything there, as long as you echo something. Like:
<?php
echo rand(1,10);
?>
When jQuery requests this page, PHP will generate a random number, and jQuery will append it to HTML.
Basically, you can respond anything there. jQuery doesn't care what you're responding.
I'm a bit of a newbie when it comes to Javascript and I am trying to find a way that I can pass a php value into a javascript/jquery function.
<script type="text/javascript">
$(document).ready(function() {
$('div#audit_admin_tabs').slideTabs({
contentAnim: 'slideH',
contentAnimTime: 600,
contentEasing: 'easeInOutExpo',
tabsAnimTime: 300,
buttonsFunction:'click',
tabSaveState:true,
autoHeight:true,
urlLinking:false
});
});
</script>
I have a value call $audit_id, the value for which is from a $_GET from the previous page. I would like to add it to the audit_admin_tabs. I have tried...
div#audit_admin_tabs<? echo $audit_id; ?>
But I realise that won't work because php is server side and javascript isn't. I have also tried echoing the whole function so I can add the php value but that didn't work either.
Thanks
div#audit_admin_tabs<?php echo $_GET['audit_id']; ?>
When you want get a get parameter you make it with $_GET['yourparam']
Greetz
You have the wrong tags. It's <?php ?> ... Not <? ?> ... As Franco has mentioned, you need to use $_GET['audit_id'] (or just use $_REQUEST['audit_id'] because that will take care of getting the value from $_GET or $_POST).
To answer your doubt about javascript being client side but PHP being server side :: Yes your understanding is correct, however in this case first PHP will do its magic so the code inside will get replaced with whatever it should be and then the resulting javascript will be served to the browser. And after that browser will execute that resulting javascript.
So, in short, it should work.
I just found out that PHP can be used in Javascript like this:
<script>
function seeTime() {
alert("<?php echo time(); ?>");
}
</script>
<button onclick="seeTime();">Click Me</button>
The above code will popup an alert message with the Unix timestamp every time the button "Click Me" is clicked. But the problem is, the time remains the same everytime you click the button until the page is reloaded.
I know AJAX is the solution but is there any other way I can update the data inside the javascript code/file without using any external php file to fetch data from it with AJAX ?
I know AJAX is the solution but is there any other way I can update the data inside the javascript code/file without using any external php file to fetch data from it with AJAX ?
Yes, just don't use PHP at all:
function seeTime() {
alert(Math.floor(new Date().getTime()/1000));
}
You are getting confused between PHP and JavaScript. PHP is not being "used" in JavaScript. PHP runs on the server and generates the page, which is then sent to the browser.
<?php echo time(); ?> is replaced with the current timestamp, then sent to the browser which runs the JavaScript.
As far as I know it's impossible, PHP code is compiled server-side, so all you return from a php compilation are static value, you must use AJAX to refresh the time value.
You could set the time with JS if you wish.
So I have the following code:
<div id="currentmotto"><?php mottoGrab($name); ?></div>
And what this does is it uses curl to screen scrape a users motto and display it on the site. What I need it to do is for that function to refresh every few seconds to see if the user has updated the motto.
I know with jquery I can use the .load('phpfile.php') but the problem then is if I put that function in that file, it no longer gets the $name variable as that is from another page.
Any ideas?
Pass name to phpfile.php via the query string:
.load('phpfile.php?name=THENAME');
Then grab the name from within phpfile.php using $_GET['name'] and stick it in the function.
OR
Make an AJAX request passing the name and update the page using javascript.
Since PHP is executed server-side, it doesn't change once the client loads the page. The only way to refresh a div without JavaScript is to reload the page.
I would try using AJAX to get the name from the other file and update the HTML with JavaScript
I got this prob. In a web page I have a DIV that display a counter ( a PHP variable, $count). I have also a JS function that change the INNERHTML property of the DIV. I'm not able to change the js var on the changing of the PHP one.
HTML
<DIV id="counter">0</DIV>
PHP
while (----) {
do_something;
$count++;
}
JS
function ChangeDiv() {
document.getElementById("counter").innerHTML = // here the value of $count;
}
setinterval("ChangeDiv()",3600);
I want to to refresh the DIV every sec inserting in it the value of $count... but doing
document.getElementById("counter").innerHTML = <?php echo($count); ?>;
is wrong 'cause it will output just the initial value of $count.
I tried to insert a parameter in the function ChangeDiv, but in that way I had to call the function every time $count changed, echoing a
echo("<script>ChangeDiv('".$count."');</script>");
...that's really not functional.
Someone knows a simpler way to do it??
You need to use some sort of AJAX implementation to get this to work.
It will allow Js to query your PHP script to get a new value for $count
It looks like you might be writing some sort of progress bar?
Note that PHP runs on the server side and generates HTML / Javascript. Javascript runs on the client side.
What you would have to do to show a progress bar like that is to output <script> snippets that update your DIV element. If you put the line
document.getElementById("counter").innerHTML = "<?php echo($count); ?>";
(notice the quotes)
into the loop, and output buffering is turned off, and the counter HTML element was output before, you will get some sort of progress bar.
However, depending on what you want kind of action you are running behind that progress bar, an Ajax driven progress indicator may be much, much more elegant.