Jump to content

change php variable with AJAX slider??


gazzamc

Recommended Posts

Hey guys i need some help on this.. i'm pretty new to php and really foreign to AJAX ..

 

Can anyone tell me how i can change a php variable with a ajax slider without refreshing the page (meaning instantly)...

 

i'll try give you a example if your not getting what i'm saying or need more info...

 

ok say i have a normal php file with a ajax slider on it...

 

and the php goes :

 

$num1 = $_GET['number'];
$num2 = 3;
$num3 = 5;

$total = $num1 + $num2 * $num3;

echo "".$num1." + ".$num2." * ".$num3." = ".$total;

 

now how could i set number which will be from the slider...

 

If you still dnt understand just let me know and i'll try clear it up...

 

Any help is appreciated.

 

 

Link to comment
Share on other sites

A simple example using jQuery UI's slider plugin.

 

$(document).ready(function() {
    $('#scrollbar').slider({
        value: center*between,
        slide: function(event, ui) {
            $('#scrollbar').slider('value', ui.value);
            $.ajax({
                type: 'post',
                url: 'yourphpscript.php',
                data: {slider: ui.value}
            });
        }
    });
});

 

yourphpscript.php

<?php

if (isset($_POST['slider'])) {
    $variable = $_POST['slider'];
}

Link to comment
Share on other sites

And just because I was bored. Here is a complete stand alone working example.

 

<?php
if (isset($_POST['slider'])) {
    @header("Content-type: application/json");
    echo json_encode(array('data' => $_POST['slider']));
    die();
} else {
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/themes/base/jquery-ui.css" type="text/css" />
    </head>
    <body>
        <div id="foo">Data returned from php will show here.</div>
        <div id="scrollbar"></div>
    </body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#scrollbar').slider({
                value: 0,
                slide: function(event, ui) {
                    $('#scrollbar').slider('value', ui.value);
                    $.ajax({
                        type: 'post',
                        url: '#',
                        datatype: 'json',
                        data: {'slider': ui.value},
                        success: function(fromphp) {
                            $('#foo').html(fromphp.data);
                        }
                    });
                }
            });
        });
    </script>
</html>
<?php } ?>

Link to comment
Share on other sites

Just to prove the data is actually coming back from php.

 

In this example, the slider sends the numbers 0-26 to php, php now responds with the corresponding letter of the alphabet.

 

<?php
if (isset($_POST['slider'])) {
    @header("Content-type: application/json");
    $alpha = range('A','Z');
    echo json_encode(array('data' => $alpha[$_POST['slider']]));
    die();
} else {
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/themes/base/jquery-ui.css" type="text/css" />
    </head>
    <body>
        <div id="foo">Data returned from php will show here.</div>
        <div id="scrollbar"></div>
    </body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#scrollbar').slider({
                value: 0,
                max: 25,
                slide: function(event, ui) {
                    $('#scrollbar').slider('value', ui.value);
                    $.ajax({
                        type: 'post',
                        url: '#',
                        datatype: 'json',
                        data: {'slider': ui.value},
                        success: function(fromphp) {
                            $('#foo').html(fromphp.data);
                        }
                    });
                }
            });
        });
    </script>
</html>
<?php } ?>

Link to comment
Share on other sites

Here it is... using your first complete example..

 

<?php
if (isset($_POST['slider'])) {
  $ptz = $_POST['slider'];
} else {
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/themes/base/jquery-ui.css" type="text/css" />
    </head>
    <body>
        <div id="foo">Data returned from php will show here.</div>
        <div id="scrollbar"></div>
    </body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#scrollbar').slider({
                value: 0,
                slide: function(event, ui) {
                    $('#scrollbar').slider('value', ui.value);
                    $.ajax({
                        type: 'post',
                        url: '#',
                        datatype: 'json',
                        data: {'slider': ui.value},
                        success: function(fromphp) {
                            $('#foo').html(fromphp.data);
                        }
                    });
                }
            });
        });
    </script>
</html>
<?php } ?>
<?php

/// Grabs user input.
$prce = 500;
$points = 2000;


// Sets discount.
$price = $prce;
$dis = '0.01';
$price2 = $price * $dis; // gets discount from price

// sets percentage per x amount of ptz.
$pts = $ptz;
$perc1 = '100'; // above 1% = 100 ptz
$total = $pts / $perc1; // users ptz divided by 100 ptz
$mtotal = $price2 * $total; // discount multiplied by result above.

// Subtracts discount from price.
$ftotal = $price - $mtotal;

echo "Points = ".$points."<br />";
echo "Price = ".$prce."<br />";
echo "Discount Price = ".$ftotal."<br />";

?>

Link to comment
Share on other sites

The reason is that the variable doesn't actually exist until you use the slider.

 

Just move all your php into the first if statement.

 

if (isset($_POST['slider'])) {
  $ptz = $_POST['slider'];
  // rest of your code
}

Link to comment
Share on other sites

The reason is that the variable doesn't actually exist until you use the slider.

 

Just move all your php into the first if statement.

 

if (isset($_POST['slider'])) {
  $ptz = $_POST['slider'];
  // rest of your code
}

 

ok i'll give that a try...

 

EDIT: After doing that the slider shows no output only "Data returned from php will show here."

Link to comment
Share on other sites

Sorry but, you might want to start actually thinking about what's going on for yourself. So far you haven't done anything that actually requires php, are you going to save this data to a database or something?

 

Anyway, you need to have the data echo'd from php back to the javascript.

 

<?php
if (isset($_POST['slider'])) {
  $ptz = $_POST['slider'];

  /// Grabs user input.
  $prce = 500;
  $points = 2000;

  // Sets discount.
  $price = $prce;
  $dis = '0.01';
  $price2 = $price * $dis; // gets discount from price

  // sets percentage per x amount of ptz.
  $pts = $ptz;
  $perc1 = '100'; // above 1% = 100 ptz
  $total = $pts / $perc1; // users ptz divided by 100 ptz
  $mtotal = $price2 * $total; // discount multiplied by result above.

  // Subtracts discount from price.
  $ftotal = $price - $mtotal;

  $arr = array(
    "Points" => $points,
    "Price" => $prce,
    "Discount" => $ftotal
  );

  @header("Content-type: application/json");
  echo json_encode($arr);

?>
}
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/themes/base/jquery-ui.css" type="text/css" />
    </head>
    <body>
        <div id="foo">Data returned from php will show here.</div>
        <div id="scrollbar"></div>
    </body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#scrollbar').slider({
                value: 0,
                slide: function(event, ui) {
                    $('#scrollbar').slider('value', ui.value);
                    $.ajax({
                        type: 'post',
                        url: '#',
                        datatype: 'json',
                        data: {'slider': ui.value},
                        success: function(fromphp) {
                            $('#foo').html("Points = " + fromphp.Points + "<br />Price = " + fromphp.Price + "<br />Discount Price = " + fromphp.Discount + "<br />");
                        }
                    });
                }
            });
        });
    </script>
</html>

 

Actually study this example and try and figure out whats going on. It would probably be easier to see whats going on if the php was in a separate file.

Link to comment
Share on other sites

I know what i want.. i want the value from the slider to be put into the variable $ptz.. then i want php to do the math and output the results... if i put it into a separate file would i need to refresh the page.. i want to achieve this without refreshing the page..

 

Thanks..

Sorry but, you might want to start actually thinking about what's going on for yourself. So far you haven't done anything that actually requires php, are you going to save this data to a database or something?

 

Anyway, you need to have the data echo'd from php back to the javascript.

 

<?php
if (isset($_POST['slider'])) {
  $ptz = $_POST['slider'];

  /// Grabs user input.
  $prce = 500;
  $points = 2000;

  // Sets discount.
  $price = $prce;
  $dis = '0.01';
  $price2 = $price * $dis; // gets discount from price

  // sets percentage per x amount of ptz.
  $pts = $ptz;
  $perc1 = '100'; // above 1% = 100 ptz
  $total = $pts / $perc1; // users ptz divided by 100 ptz
  $mtotal = $price2 * $total; // discount multiplied by result above.

  // Subtracts discount from price.
  $ftotal = $price - $mtotal;

  $arr = array(
    "Points" => $points,
    "Price" => $prce,
    "Discount" => $ftotal
  );

  @header("Content-type: application/json");
  echo json_encode($arr);

?>
}
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/themes/base/jquery-ui.css" type="text/css" />
    </head>
    <body>
        <div id="foo">Data returned from php will show here.</div>
        <div id="scrollbar"></div>
    </body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#scrollbar').slider({
                value: 0,
                slide: function(event, ui) {
                    $('#scrollbar').slider('value', ui.value);
                    $.ajax({
                        type: 'post',
                        url: '#',
                        datatype: 'json',
                        data: {'slider': ui.value},
                        success: function(fromphp) {
                            $('#foo').html("Points = " + fromphp.Points + "<br />Price = " + fromphp.Price + "<br />Discount Price = " + fromphp.Discount + "<br />");
                        }
                    });
                }
            });
        });
    </script>
</html>

 

Actually study this example and try and figure out whats going on. It would probably be easier to see whats going on if the php was in a separate file.

Link to comment
Share on other sites

I know what i want.. i want the value from the slider to be put into the variable $ptz.. then i want php to do the math and output the results...

 

I'm not sure you understand what Ajax is. In this example, data is sent via Javascript back to the server to php, php then does some calculations and sends the data back to Javascript, Javascript then displays the results.

 

if i put it into a separate file would i need to refresh the page.. i want to achieve this without refreshing the page..

 

No.

 

Have you looked at the last example I have posted? Its does what your after.

Link to comment
Share on other sites

To be honest i don't , sorry. so in the last example if i just split them up into two file it will work?

 

I know what i want.. i want the value from the slider to be put into the variable $ptz.. then i want php to do the math and output the results...

 

I'm not sure you understand what Ajax is. In this example, data is sent via Javascript back to the server to php, php then does some calculations and sends the data back to Javascript, Javascript then displays the results.

 

if i put it into a separate file would i need to refresh the page.. i want to achieve this without refreshing the page..

 

No.

 

Have you looked at the last example I have posted? Its does what your after.

Link to comment
Share on other sites

Post the exact code you are using.

 

ok here it is..

 


<?php
if (isset($_POST['slider'])) {
  $ptz = $_POST['slider'];

  /// Grabs user input.
  $prce = 500;
  $points = 2000;

  // Sets discount.
  $price = $prce;
  $dis = '0.01';
  $price2 = $price * $dis; // gets discount from price

  // sets percentage per x amount of ptz.
  $pts = $ptz;
  $perc1 = '100'; // above 1% = 100 ptz
  $total = $pts / $perc1; // users ptz divided by 100 ptz
  $mtotal = $price2 * $total; // discount multiplied by result above.

  // Subtracts discount from price.
  $ftotal = $price - $mtotal;

  $arr = array(
    "Points" => $points,
    "Price" => $prce,
    "Discount" => $ftotal
  );

  @header("Content-type: application/json");
  echo json_encode($arr);
}
?>

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/themes/base/jquery-ui.css" type="text/css" />
    </head>
    <body>
        <div id="foo">Data returned from php will show here.</div>
        <div id="scrollbar"></div>
    </body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#scrollbar').slider({
                value: 0,
                slide: function(event, ui) {
                    $('#scrollbar').slider('value', ui.value);
                    $.ajax({
                        type: 'post',
                        url: '#',
                        datatype: 'json',
                        data: {'slider': ui.value},
                        success: function(fromphp) {
                            $('#foo').html("Points = " + fromphp.Points + "<br />Price = " + fromphp.Price + "<br />Discount Price = " + fromphp.Discount + "<br />");
                        }
                    });
                }
            });
        });
    </script>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.