0
    /**
 * Created by quantumveil on 2/10/15.
 */
$(document).ready(function(){


    $('a').on('click',function(){
//$href=$(this).attr('href');
    //console.log($href);
var $convoid=$(this).attr('id');
        console.log($convoid);
    //$('#convoarea').load($href);
        $.get('inbox.php',{convoid:$convoid},function(data){$('#convoarea').html(data);//the callback sets whati t had to

                //now add the on click handler to send button
                $('#send').on('click',function(){
                    var $msgbody=$('#sendbody').val();

                    console.log($msgbody);

///now what i have to do is sent this $msgbody to the inbox.php thru get k

                    $.post('inbox.php',{sendbody:$msgbody,convoid:$convoid},function(data){
                        $('#sendbody').before(data);
                            console.log('here'+$msgbody);
                    });//the callback function will append it to other messages :3
                    return false;
                });


        }//callback ends hre
        );
    return false;

});

    //for send button


});

Hi. I'm trying to code a social networking site and I want the inbox to be responsive and dynamic. I've recently started learning Jquery for the purpose being. I've googled this before and found no help and so it brings me here on this community. The above Javascript/Jquery code is supposed to pass some post parameters when the SEND button is clicked. Another file, inbox.php, is to receive those and work appropriately. Now here's what bugs me. The callback function to $.post() is executed, so I'm assuming the parameters are being passed to inbox.php. But when I try accessing them in inbox.php using following line

if($msgbody=get_post('sendbody')&&$convoid=get_post('convoid'))

I only receive a value of 1 and not the message's actual body. Here's how the function get_post is defined

function get_post($var1){
    if(isset($_POST[$var1])){$var=$_POST[$var1];
        return filter($var);}
    else return 0;

}

I've tried accessing them directly through $_POST['sendbody'] but an error of undefined index is being generated. Any help will be highly appreciated. (PS the other call to .get() in the beginning of this js file is passing the parameters so there's nothing wrong with file paths)

EDIT: It's fixed but I want an explanation. All I did was in the inbox.php changed the first line which was

if($msgbody=get_post('sendbody') 

to

if(isset($_POST['sendbody'])$msgbody=$_POST['sendbody']

Now all I can wonder is if it has something to do with filter() function in the definition of my get_post() function. Anybody?

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 2
    `if($msgbody=get_post('sendbody')&&$convoid=get_post('convoid'))` You're using single equal `=` (assignment), when you want to use double equal `==` for comparison. – Jeremy Thille Feb 10 '15 at 15:36
  • `if(isset($_POST['sendbody'])$msgbody=$_POST['sendbody']` you're missing a closing parenthesis (one-liners are never easy to read) – Jeremy Thille Feb 10 '15 at 15:38
  • @JeremyThille He is using the result of the assignment (0 if not defined, something otherwise) as the boolean value, I think it's correct in that case (unless the passed value is 0, then this fails) – Alvaro Montoro Feb 10 '15 at 15:38
  • Ah right (although weird) – Jeremy Thille Feb 10 '15 at 15:39
  • exactly. If $var=get_post('key'), the value of equation should be non-zero and at the same time $var should be set as the key-value. This just doesn't make sense to me. The get_post() method has worked flawlessly until now, then why did it not work with parameters passed here? – Robino Collaso Feb 10 '15 at 15:45
  • function filter($temp){ global $con; $var=$con->real_escape_string($temp); return $var; } – Robino Collaso Feb 10 '15 at 15:46
  • I think you should not use $_GET and $_POST in your functions, this is a good page to visit http://stackoverflow.com/questions/1354691/php-get-and-post-in-functions – ismnoiet Feb 10 '15 at 16:21

0 Answers0