0

On my website I am making a dialog form with the jQuery ui framework.

The dialog requests a topic, review content, name and city which should be inserted into mySQL. However when I want to pass the variables to PHP there is no value in the variables.

Here is the entire js function that handles the dialog:

$( "#dialog-form" ).dialog({
    autoOpen: false,
    height: 600,
    width: 550,
    modal: true,
    buttons: {
        "Skriv Review": function() {
            var bValid = true;
            allFields.removeClass( "ui-state-error" );

            bValid = bValid && checkLength( topic, "topic", 3, 16 );
            bValid = bValid && checkLength( review, "review", 1, 10000 );
            bValid = bValid && checkLength( name, "name", 1, 25 );
            bValid = bValid && checkLength( city, "city", 1, 16 );

            bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Bruger skal være a-z, 0-9, underscores, begynde med et bogstav." );

            if ( bValid ) {
                $( "#users tbody" ).append( "<tr>" +
                    "<td>" + topic.val() + "</td>" + 
                    "<td>" + review.val() + "</td>" + 
                    "<td>" + name.val() + "</td>" +
                    "<td>" + city.val() + "</td>" +
                "</tr>" ); 

                var data = {
                    topic: topic.val(),
                    review: review.val(),
                    name: name.val(),
                    city: city.val()
                }

                $.ajax({
                    type : 'POST',
                    url : 'bruger-reviews.php',
                    dataType : 'json',
                    data: data,
                    success : function(data){
                        //success
                    }
                });
                $( this ).dialog( "close" );
            }
        },
        Cancel: function() {
            $( this ).dialog( "close" );
        }
    },
    close: function() {
        allFields.val( "" ).removeClass( "ui-state-error" );
    }
});

And here is how I receive the data in PHP:

$ins_topic = $_POST['topic'];
$ins_review = $_POST['review'];
$ins_name = $_POST['name'];
$ins_city = $_POST['city'];

I have no issue displaying the values like it is done in the demo from jQuery UI.

There is no data when I $_GET or $_POST the variables. Why is it not possible for me to pass the values to PHP? Is there another method I can try?

Thanks.

3
  • can you attach your full javascript code? because there is still not clear on your question, if necessary attach your php also Commented Aug 28, 2012 at 13:54
  • bungdito, I have attached the entire js function along with the PHP. Commented Aug 29, 2012 at 7:09
  • I have tried to use the json_decode method and access the data, but still nothing. Commented Aug 29, 2012 at 7:27

1 Answer 1

1

You should take the values to an object or variables before close the dialog.

this code removes the removes the dialog from the DOM; $( this ).dialog( "close" );

use this;

if ( bValid ) {
$( "#users tbody" ).append( "<tr>" +
"<td>" + topic.val() + "</td>" + 
"<td>" + review.val() + "</td>" + 
"<td>" + name.val() + "</td>" +
"<td>" + city.val() + "</td>" +
"</tr>" ); 
var data = {
    topic: topic.val(),
    review: review.val(),
    name: name.val(),
    city: city.val()
}
$( this ).dialog( "close" );

than use this data object on ajax request

$.ajax({
                        type : 'POST',
                        url : 'bruger-reviews.php',
                        dataType : 'json',
                        data: data,
                        success : function(data){
                            //success
                        }
                    });
Sign up to request clarification or add additional context in comments.

2 Comments

Still can't seem to receive the values in PHP. Do I need to refresh the page after the ajax function has run, in order to do the $_POST?
refreshing the page is against the main reason of using the ajax. Can you console.log(data) right before the $.ajax statment. And try to echo this request on php ; $.ajax({type:'post', url : 'bruger-reviews.php',data:{test:'test'},success: function(res){console.log(res);}})

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.