2

Ok guys hope you can help me with this one as well. I am not to crazy knowledgeable with jQuery and AJAX and probably I am missing something very simple. I am not able to pass more than 1 variable. I have tried several different things amongst closest to what I have working is this but even that didn't work.

jQuery

$("#bodymes ul li span[contenteditable=true]").blur(function(){
            var weight_id = $(this).attr("id") ;
            var weightbody_mes = $(this).text() ;
            $.post( "../includes/bodymes_weight.php", { weightbodymes: weightbody_mes })  
                .done(function( data ) {   
                    $(".weightsuccess").slideDown(200); 
                    $(".weightsuccess").html("Weight Updated successfully"); 
                    if ($('.weightsuccess').length > 0) {
                        window.setTimeout(function(){
                            $('.weightsuccess').slideUp(200);
                        }, 4000);
                    }  
                    // alert( "Data Loaded: " + data);
            });
        });

So basically If I run this it will work perfectly fine and my PHP script will process it. Notice that when I go and enable to alert me and show data loaded it shows correct info (info from weightbodymes) and I am able to update db. But as soon as I add another variable { weightbodymes: weightbody_mes, weightid: weight_id } it won't show data loaded in alert box (if I try to show info from both variables it's working but it only submits one var only to PHP which is:

    $weightid = $_POST['weightid'];
    $weight = $_POST['weightbodymes'];

    if ($insert_stmt = $mysqli->prepare("UPDATE body SET body_weight=? WHERE body_id='$weightid'")) {
            $insert_stmt->bind_param('s', $weight);
            // Execute the prepared query.
            if (! $insert_stmt->execute()) {
                header('Location: ../index.php?error=Registration failure: INSERT nwprogram1');
            }
    }

Hope you can tell me where I am making a mistake and how to correct it. THANK YOU IN ADVANCE!

4
  • Add futher properties to the data object: { weightbodymes: weightbody_mes, weightid: weight_id, foo: 'bar' } Commented Feb 11, 2015 at 15:39
  • Send something like this {'data':{'var_1': 'value1', 'var_2' : 'value2'}} .Then access it by $_POST['data']['var_1'], $_POST['data']['var_2'] Commented Feb 11, 2015 at 15:39
  • This should have what you want stackoverflow.com/questions/8191124/… Commented Feb 11, 2015 at 15:40
  • I tried first and third answer but it seems not to work. ill give it a shot couple more times. Thank you! Commented Feb 11, 2015 at 16:04

1 Answer 1

6

POST AJAX REQUEST using JS / JQUERY. The below is the syntax I use for all my AJAX calls.

var first = 'something';
var second = 'second';
var third = $("#some_input_on_your_page").val();


///////// AJAX //////// AJAX //////////
    $.ajax({
        type: 'POST',
        url:  'page_that_receives_post_variables.php',
        data: {first:first, second:second, third:third},
        success: function( response ){
            alert('yay ajax is done.');
            $('#edit_box').html(response);//this is where you populate your response
        }//close succss params
    });//close ajax
///////// AJAX //////// AJAX //////////

Code for the PHP page page_that_receives_post_variables.php

<?php
$first = $_POST['first'];
$second = $_POST['second'];
$third = $_POST['third'];

echo 'now do something with the posted items.';
echo $first; //etc...
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Hmmm... this seemed like a solution but it is not working for some reason it is still passing only one variable. So for example, in a response I am only getting weightbodymes although I am passing both variables my data line looks like this: data: {weightid:weight_id, weightbodymes:weightbody_mes, third:third}, (I left third element and assigned variable as well but didn't try to call it with PHP). Any other suggestion?
I made it to work with your solution. Also voted up. THANK YOU! @Stephen Carr
what was the issue? For debugging, before you past post variables in the ajax, just alert(your_variables) so you can see if you are actually getting something. Usually helps me.
I used alert() and gave me correct info but issue was with PHP as I have if statement and the part I posted in this question was part of "else" and in both if and else I placed the same variable while I was trying different solutions and forgot to change it so it caused the wrong records to update and whole bunch of other mess. Thank you again! @Stephen Carr

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.