0

I have a simple setup that takes a hex value from a color picker converts it to RGB and then sends it to a PHP script from the HTML. The recieving file is not echoing and it is not refreshing either. I probably am doing something wrong but wanted to run it by someone just in case.

Java/Jquery

function hexToRgb(hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : null;
}
$(document).ready(function() {
    var picker = $.farbtastic('#picker');
    picker.linkTo(function onColorChange(color) {
        var finalcolor=hexToRgb(color);
        console.log(finalcolor,"helloworld");

        $.post("imagedisplay.php", {var_value: finalcolor});

    });
});

RECIEVING PHP

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $hex=$_POST['var_value'];
    echo '$hex';
}
?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Did you check with your console to make sure the $_POST headers were being sent properly? Did you try `var_dump($_POST);` at the very top of the PHP script? – Michael Tallino Jul 28 '15 at 01:04
  • 2
    For one, `echo '$hex';` will literally print just that. Do `echo $hex;` instead. – Anonymous Jul 28 '15 at 01:06
  • You have no callback function in your `$.post`, so it doesn't do anything with the response from PHP. – Barmar Jul 28 '15 at 01:08
  • [difference between single and double quotes in PHP](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Barmar Jul 28 '15 at 01:09

1 Answers1

0

When you use AJAX, the page is not refreshed automatically (that's generally the reason you use AJAX instead of normal form submission). You need a callback function in $.post to do something with the response from PHP:

$(document).ready(function() {
    var picker = $.farbtastic('#picker');
    picker.linkTo(function onColorChange(color) {
        var finalcolor=hexToRgb(color);
        console.log(finalcolor,"helloworld");

        $.post("imagedisplay.php", {var_value: finalcolor}, function(response) {
            alert('PHP said: ' + response);
        });
    });
});

In your PHP, $hex is an associative array. You can't echo an array, use:

print_r($hex);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Ok the alert back said PHP Said: Array Not Sure what that means But I am guessing something is not working –  Jul 28 '15 at 01:19
  • That will happen if you try to echo a variable that contains an array rather than a string. – Barmar Jul 28 '15 at 01:20
  • With the PHP script you posted, I would expect it to alert `PHP said: $hex`. I don't know where `Array` is coming from, unless your PHP is different. – Barmar Jul 28 '15 at 01:21
  • It did that once, then it just kept saying array. All I am passing is a RGB converted from a hex value. –  Jul 28 '15 at 01:23
  • I see what's going on. `hexToRgb` returns a Javascript object. When this gets sent to PHP, `$_POST['var_value']` is an associative array. So `echo $hex` prints `Array`. – Barmar Jul 28 '15 at 01:23
  • Use `print_r($hex)` instead of `echo $hex`. – Barmar Jul 28 '15 at 01:25
  • [output of php](http://preview.88kcikfcuw4mfgvi8ckrxvjrndu6jemi01t025rhda6skyb9.box.codeanywhere.com/) There we go now it's outputting right. –  Jul 28 '15 at 01:29