0

What I'm trying to do is create some jQuery code that will initiate some PHP code and reload the page when I click on an image. I've created the code below but am confused at what type of event handler in jQuery is necessary to initiate PHP code. I suspect this is borderline AJAX, with the exception being that this is synchronous. Here is the code below.

<script src="http://code.jquery.com/jquery-latest.js"></script>

<style>

img:hover { border:1px solid #021a40; margin: 1px; padding: 1px; color: #000000; 

cursor:pointer;};

</style>

<script>

$(document).ready(function(){

$('img').on('click',"img",function(){

$(this).slideUp();

What do I put here?????

});

});

</script> 

<img  src="image.gif" width="50" height="50" />
4
  • 1
    Why does this have to be jQuery/javascript? Can't you just use a direct link to the PHP-file? Commented Jul 19, 2012 at 22:36
  • what is that dear php code you want to run? Commented Jul 19, 2012 at 22:38
  • I want the .slideUP() function to work when I click on the image. My fear is the if I create a direct link to the php file then the effects from the jquery will not be seen. Commented Jul 19, 2012 at 22:39
  • The php code is an mysql query that updates a field in my database that contains information of the image on the page and a small snippet of code that will send the user to a new page. Commented Jul 19, 2012 at 22:41

2 Answers 2

1

With no ajax try:

$(document).ready(function()
{
   $('img').on('click',"img",function()
   {
       $(this).slideUp('slow',function () 
       {
          window.location = "url/to/phpfile.php?data="+yourdata
       });
   });
});

Then in the php file, do your updates and output or redirect to another page.

Regarding your comment above, the second parameter to slideUp() is a callback function once the animation is complete so you should get the visual feedback before the page transitions.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is what i think you can do.

$(document).ready(function(){
  $('img').on('click',"img",function(){
   $(this).slideUp('slow',function () {
      $.ajax({/* your ajax call here will run when the sliding is done */})
   });
  });
});

4 Comments

There is one problem, I can't run AJAX. I'm testing all of this on XAMPP so AJAX is out of the question. I'm ok with redirecting the user to another page. Thanks for the help though.
@jason328 just so you know, XAMPP does not have a problem with ajax. you can run ajax anywhere
The problem is that you have to put the files that you want to run into another web server when you call them with ajax, correct?
That is incorrect, XAMPP is a webserver, i understand using a redirect solve your problem, but i think you need to learn more on how ajax work. AJAX MSD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.