1

I have not been able to find a simple example of ajax with single variable, everything on here is made way too complicated for an AJAX beginner. I've watched about 4 different YouTube videos on the topic, but can't seem to get it right. I have the src of an image in a variable like so with a JavaScript..

<img alt="" src="blah.jpg" style="height: 276px; width: 200px" id="imgClickAndChange1" onclick="changeImage(this)" />

<script language="javascript">
function changeImage(imagePass) {

var num = Math.floor((Math.random() * 48) + 1);
var n = num.toString();
var numImg = n.concat(".jpeg");
var string = "/Images/Folder/"
var final = string.concat(numImg);
imagePass.src = final;
//(this is where I want to pass the variable imagePass.src or "final" to a php script w/ Ajax)

Here is my php script:

<?php>
include_once "code.php";  //connecting to database
$s = (this is where I want the variable to be posted);
$temp = explode('/', $s);
$temp2 = explode('.', $temp[count($temp) - 1]); //this is getting the variable I want from the variable sent(which is actually a number)
$float = (int)$temp2; //changing the number (which is a string) to an int
mysql_query("UPDATE Variable SET `upVote` = `upVote`+1 WHERE id= (variable here)) "  //Making a row in my database w/ the id of the variable add 1 to the count
?>

How would I go about posting and sending this w/out a page refresh? AJAX is really confusing me so a working implementation to get me started on this would be great, Thanks a lot.

//Let's just assume the php page where the script is located is called 'hello.php'

1

1 Answer 1

2

To use ajax, try this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">

function changeImage(imagePass) {

        var num = Math.floor((Math.random() * 48) + 1);
        var n = num.toString();
        var numImg = n.concat(".jpeg");
        var string = "/Images/Folder/"
        var final = string.concat(numImg);
        imagePass.src = final;
        $.ajax({
          url : 'hello.php',
          type: 'post',
          data : {'final':final},
          success: function()
              {
                    alert('Success!');
              }
      });
   }
</script>

PHP script (hello.php):

<?php>
include_once "code.php";  //connecting to database
$s = $_POST['final'];
$temp = explode('/', $s);
$temp2 = explode('.', $temp[count($temp) - 1]); //this is getting the variable I want from the variable sent(which is actually a number)
$float = (int)$temp2; //changing the number (which is a string) to an int
mysql_query("UPDATE Variable SET `upVote` = `upVote`+1 WHERE id= (variable here)) "  //Making a row in my database w/ the id of the variable add 1 to the count
?>
Sign up to request clarification or add additional context in comments.

2 Comments

How would the php page look? - Something like $s = _POST ?
Worked perfect, Thanks for your help!

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.