0

hi guys i been fighting with this im trying to pass a jquery value to a php variable

this is my code

$('body').on('change','#select_ref',function(){
    var name = $(this).val();
    var dataString = "name=" + name;
    $.ajax ({
        type: "POST",
        url: "referencia_select.php",
        data: dataString,
        dataType: 'json',
        success: function(data) {
            // console.debug(data);
            $('#vendedor').val(data.id_vendedor);
            $('#name').val(data.name);
            $('#email').val(data.email);
            $('#compa').val(data.lastname);
            $('#rfc').val(data.rfc);
            $('#phone').val(data.phone);
            $('#fax').val(data.fax);
            $('#address').val(data.address);
            $('#estado').val(data.estado);
            $('#city').val(data.city);
            $('#zipcode').val(data.cp);
            $('#productos').val(data.productos);
        }

    });
});

i already try sending it with cookies

var productos = data.productos;
document.cookie = "products=" + productos ;
console.log(productos);

on my php

try

$variable = $_POST['products']; 
$variable = $_COOKIE['products'];

and different methods with not luck any subjection to make this work for my application

im able to chage a input with the data

<input type="text" name="productos" id="productos" value="" data-array="12,12,Productos" />

but i really need it in a php variable thanks a lot

1
  • In your dataString you did not pass a parameter named products that is why you cant get the value. Try to display your posted values like var_dump($_POST) in your php to help you understand what did you pass in your ajax post request. Commented Nov 5, 2019 at 1:06

1 Answer 1

1

Send it in the AJAX data.

$('body').on('change','#select_ref',function(){
    var name = $(this).val();
    var dataObj = {name: name, products: data.productos};
    $.ajax ({
        type: "POST",
        url: "referencia_select.php",
        data: dataObj,
        dataType: 'json',
        success: function(data) {
            // console.debug(data);
            $('#vendedor').val(data.id_vendedor);
            $('#name').val(data.name);
            $('#email').val(data.email);
            $('#compa').val(data.lastname);
            $('#rfc').val(data.rfc);
            $('#phone').val(data.phone);
            $('#fax').val(data.fax);
            $('#address').val(data.address);
            $('#estado').val(data.estado);
            $('#city').val(data.city);
            $('#zipcode').val(data.cp);
            $('#productos').val(data.productos);
        }

    });
});

Then it will be in $_POST['products'].

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

Comments

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.