...

View Full Version : AJAX help



venom6pak
01-24-2011, 04:27 AM
hiya guys :)

i have a problem, what im doing is making a timestamp on a web page with php, got that bit sorted using gmdate(); and time(); with echos printing out the format but now i want it not to show on the page unless a user clicks on a button, lets say a drop down box with options like show time in hours and mins or show time in days and hours etc

so the echo i output i will output in diff formats from the same timestamp using a select drop down box

i want the user to select `show time in days and hours` and then it shows them the echo that represents that format

how do i go about this? ive done a bit of reading about it re javascript and php and found out i need to use ajax, i found one example that i couldnt get working :(

any help peeps? :) :) :thumbsup:

FrostedFlakes
01-24-2011, 06:03 AM
This doesn't exactly do what you want but it is a start.



JQUERY:
$("#clicked").click(function () {
$("#target").show("slow");
});


Hidden DIV:
<div id="target" style="display:none"><?php echo $time; ?></div>
Button:
<input type="button" id="clicked'>


Also you could also use .toggle() instead of .show()

For a drop-down menu: (remember that you'll need to load the value of the item on the list or else it won't work right)


JQUERY:
$("#myList").change(function() {
$("#target").show();
return false;
});

ShaneC
01-24-2011, 06:08 AM
Seeing as your problem has more to do with the Javascript part of AJAX, as opposed to the PHP component, I have moved your question here to the AJAX Category.

Hopefully you should get more spot on advice here!

venom6pak
01-24-2011, 01:53 PM
thanks frosted flakes that looks exactly what im looking for forgive me for being so amateurish but ive tried to use that method as follows and its telling me Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$' in index.php

<?php
$("#clicked").click(function () {
$("#target").show("slow");
});

function time(){
echo gmdate('D, d M Y H:i:s T');
}
?>

<html>
<body>

<div id="target" style="display:none"><?php echo $time; ?></div>
<input type="button" id="clicked'>
</body>
</html>

where am i going wrong? :) :thumbsup:

Spudhead
01-25-2011, 10:46 AM
You've mixed up Javascript and PHP: the jQuery code that FrostedFlakes posted is Javascript; don't wrap it in <?php ?> tags, wrap it in HTML <script> tags.

venom6pak
01-25-2011, 07:29 PM
ah man i still cant get it working, if i put this code into blank php page i get the time i want `<?php echo gmdate('D d/m/Y H:i:s a'); ?>`

if i use this below what ive tried to put together from frosted flakes example i hust get a blank screen with `<input type="button" id="clicked'>` displayed in browser

<html>
<script>
$("#clicked").click(function () {
$("#target").show("slow");
});
</script>

<div id="target" style="display:none"><?php echo gmdate('D d/m/Y H:i:s a'); ?></div>
<input type="button" id="clicked'>
</html>

i then moved the closing div tag to after the input button part to see if that resolved it as shown below but this just left a blank screen

<html>
<script>
$("#clicked").click(function () {
$("#target").show("slow");
});
</script>

<div id="target" style="display:none"><?php echo gmdate('D d/m/Y H:i:s a'); ?>
<input type="button" id="clicked'> </div>
</html>


i still need a method to display a time in php with a buttonclick :( :(

devnull69
01-25-2011, 07:37 PM
if i use this below what ive tried to put together from frosted flakes example i hust get a blank screen with `<input type="button" id="clicked'>` displayed in browser


<html>
<script>
$("#clicked").click(function () {
$("#target").show("slow");
});
</script>

<div id="target" style="display:none"><?php echo gmdate('D d/m/Y H:i:s a'); ?></div>
<input type="button" id="clicked'>
</html>


Do you mean this is the complete code for your page? What about head and body elements? They are essential ... so the minimum would be something like this:


<!DOCTYPE html>
<html>
<head>
<script>
$(document).ready(function() {
$("#clicked").click(function () {
$("#target").show("slow");
});
});
</script>
</head>
<body>
<div id="target" style="display:none"><?php echo gmdate('D d/m/Y H:i:s a'); ?></div>
<input type="button" id="clicked" />
</body>
</html>

Note: Mind the closing quote of the input ... it was a single quote and had to be changed to a double quote.

By the way: You are not trying to "display a time in PHP". You are using Javascript to display a previously hidden text (that originally was the output of a PHP script which finished already).

venom6pak
01-25-2011, 08:20 PM
thanks for your reply devnull i was at the time doing just that trying to iron out any red herrings so to speak anyway i now have the code on the server exactly like this

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
$("#clicked").click(function () {
$("#target").show("slow");
});
</script>

</head>
<body>

<div id="target" style="display:none"><?php echo gmdate('D d/m/Y H:i:s a'); ?> </div>
<input type="button" id="clicked">

</body>
</html>


thing is all that i get displayed is a tiny little grey button that when clicked doesnt do anything, do you happen to know how to resolve this? :)

devnull69
01-25-2011, 09:16 PM
Please compare your code with the code I provided earlier. There is one important difference:

Put this around your javascript


$(document).ready(function() {
// your code here
});


It will make sure the button exists before you try to assign the click handler to it.

venom6pak
01-27-2011, 02:51 PM
hi again devnull :) thanks for your reply i must be starting to be very annoying now lol haha but im trying ! :)

this is how the code is on my page

<!DOCTYPE html>
<html>
<head>
<script>
$(document).ready(function() {
$("#clicked").click(function () {
$("#target").show("slow");
});
});
</script>
</head>
<body>
<div id="target" style="display:none"><?php echo gmdate('D d/m/Y H:i:s a'); ?></div>
<input type="button" id="clicked" />
</body>
</html>


but i cannot get it to do anything it just displays to me a tinygrey button

:( :(:confused:

devnull69
01-27-2011, 02:58 PM
First of all: You should definitely get used to debugging ... on Firefox install the Firebug add-on and take a look at what you get in the HTML source. It should display the timestamp inside the (hidden) div. If that is not the case, the problem is with PHP and not jQuery. But only you can tell us ....

And: Well ... you have been using $(), so everybody is expecting that you have the necessary <script src="....../jquery.xxxx.js"> included. Do you? Otherwise the urge to debug would be even greater ;-)

venom6pak
01-27-2011, 03:13 PM
ahha yea thanks devnull ive changed it about that many times i forgot to put the source link back in

it works now what a legend you are :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum