Piotr Szymański
Piotr Szymański

Reputation: 1

Jquery doesn't take the val()

I have a little problem with the jQuery - In jquery ui confirm dialog i have 2 inputs: first - hidden with precise value="" second - input text - and here is the problem.

When I click "ok" the script sending me only first value, second is empty. But when I write the value="123" there two are sending.

HTML:

<input type="text" value="" class="MyIn" id="MyIn" name="MyIn" >
<input type="hidden" value="<?=$r['order_id'];?>" class="orderid" id="orderid" >

JavaScript:

  $("a.opener").click(function (e) {
        $("#dialog-message").dialog({

            buttons: {
                Ok: function () {
                    var sendNumber = $("input#MyIn").val();
                    var orderid = $("input#orderid").val();

                    var dataString = 'orderid=' + orderid + '&sendNumber=' + sendNumber;

                    $.ajax({
                        type: "POST",
                        url: "moduls/send.php",
                        data: dataString,
                        success: function () {
                            $("#dialog-message").dialog("close");
                        }
                    });
                    return false;

                }
            }
        });
    });

Upvotes: 0

Views: 140

Answers (3)

TheCarver
TheCarver

Reputation: 19703

It probably won't make much difference but it can be good practice, when sending data through AJAX, to include a DATATYPE, like this:

$.ajax({
  type: "POST",
  url: "moduls/send.php",
  data: dataString,
  dataType: 'text',
  success: function () {
    $("#dialog-message").dialog("close");
  }
});

Upvotes: 1

Fresheyeball
Fresheyeball

Reputation: 30015

I very much suspect that <?=$r['order_id'];?> is returning an empty string. If this is the case it will not be sent through. Also dataString is not the preferred way of handing params in jQuery. Bellow is with a catch for this scenario:

$("a.opener").click(function (e) {
    $("#dialog-message").dialog({

        buttons: {
            Ok: function () {

                // if sendNumber fails or evaluates to false, 
                // it will be replaced with 'null', insuring its sent anyway
                // and you can trap on the serverside.
                var sendNumber = $("input#MyIn").val() || 'null';
                var orderid = $("input#orderid").val();

                var postData = { orderid : orderid, sendNumber : sendNumber};

                //shorthand of extra niceness
                $.post('moduls/send.php', postData, function(){
                     $("#dialog-message").dialog("close");
                });
                return false;

            }
        }
    });
});

Upvotes: 1

Robert
Robert

Reputation: 8767

Your current jQuery code should be working as the refence of the hidden input element's value is valid.

Demo: http://jsfiddle.net/3qpV4/

Try alerting the value to ensure it is being set properly. If it isn't, then you know that the problem is in your PHP variable expansion. If it does alert, then the issue is with your PHP file send.php.

Upvotes: 0

Related Questions