How to return ajax php variable

Asked

Viewed 886 times

0

I am developing a form via ajax/php, when I click on the desired item the form sends the data to ajax and displays in div, but I need to return a value but I am not getting.

JS:

//script do form
$("#copPasta").focus(function(){
var val = new Array();
$('.check:checked').each(function(){
    val.push($(this).val());
});
$.ajax({
    url:'copPasta.php',
    type:'GET',
    data:'valor=' + val,
    success:function(data){
        $('.exibeCopPasta').html(data);
    }
});
return false;
});

PHP:

        <?php //ajax
    if( isset( $_GET['valor'] ) ){
        $a = explode(',' , $_GET['valor']);
        foreach( $a as $value ){
            $a_value = explode('/' , base64_decode($value));
            $f_value = array_pop($a_value);
            echo $f_value."<br />";
            //gera array descriptografado
            $array[] = base64_decode($value);
        }
    }
    $c = 1;
    return $c;
    ?>

$c in php I would need to confirm the reading and enable the function, then then in the main form if it is set the $c variable performs the copy function. What I’m doing wrong?

  • Maybe not the best practice, but instead return use the echo

  • Checking that if the variable is set is on the same ajax page?

  • 1

    You need to $c, Isn’t that right? From what I understand of your comment: return the value with echo and then check the ajax page

  • then, I have the form as main, and an ajax call to treat and return in a div, but I need to return a variable $c, by echo I can’t validate in a form, it can be via get, post, anything, but I need to return some value in a variable coming from ajax.

  • You absolutely need to use echo. With Return ajax does not take the return.

  • As I said before, use echo whatever you want to return.Anything "displayed in php" will return to the variable data js as plain text.I would recommend setting the Reply header and working with json.

  • It may not be best practice, but I found a way. <input type="Hidden" value="c" name="action" /> I applied inside php after ajax and was able to redeem via $_REQUEST

  • That one data:'valor=' + val, is to be a single string ?

  • Commonly used date: {value: val} where "value" will be the name of the key in the array on the server side and when called "$_GET['value']" will be returned from the value passed by the val variable on the front end side

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.