0

I currently have a several input fields and using ajax to assign values to the php variables. In my javascript code I have function saveThem which stores the values. At the end there is a button that will store the values in a MySQL database. Now with the php, how can I disable the button if one of the variables happens to be empty or NULL ? Mockup SITE

select: function saveThem() {

                    var name = $.trim($("#name").val());
                    var age = $.trim($("#age").val());
                    var phone = $.trim($("#phone").val());
                    var email = $.trim($("#email").val());
                    var job = $.trim($("#job").val());
                    var hobby = $.trim($("#hobby").val());

                    var dataString = 'name='+name+'&age='+age+'&phone='+phone+'&email='+email+'&job='+job+'&hobby='+hobby;

                          $.ajax({
                              type: "POST",
                              url: 'posting.php',
                              data: dataString,
                              dataType: "html",
                              success: function(data) {
                                /*if(data.response){ alert(data.message); }*/
                                $("#inputResult").html(data);
                              }
                          });
                        }

PHP

    $name = (isset($_POST['name'])) ? strip_tags($_POST['name']) : NULL;
    $age = (isset($_POST['age'])) ? strip_tags($_POST['age']) : NULL;
    $phone = (isset($_POST['phone'])) ? strip_tags($_POST['phone']) : NULL;
    $email = (isset($_POST['email'])) ? strip_tags($_POST['email']) : NULL;
    $job = (isset($_POST['job'])) ? strip_tags($_POST['job']) : NULL;
    $hobby = (isset($_POST['hobby'])) ? strip_tags($_POST['hobby']) : NULL;

    echo ('<br>'.$name.'<br>'.$age.'<br>'.$phone.'<br>'.$email.'<br>'.$job.'<br>'.$hobby);
  • when do you want this happen? –  Feb 06 '13 at 06:08
  • You can disable buttons with http://www.w3schools.com/tags/att_input_disabled.asp, and you can check for empty with http://stackoverflow.com/questions/154059/what-is-the-best-way-to-check-for-an-empty-string-in-javascript – fionbio Feb 06 '13 at 06:09
  • @Akam well going by my example, after the user has input all data into the fields –  Feb 06 '13 at 06:19

1 Answers1

1

You can disable button in javascript if any of the variable happens to be NULL or empty.

if($("#name").val() == '' || $("#age").val() == '' || .... so on )
{
   $("#buttonid").attr('disabled',true);
}
else
{
   $("#buttonid").attr('disabled',false);
}

var dataString = 'name='+name+'&age='+age+'&phone='+phone+'&email='+email+'&job='+job+'&hobby='+hobby;
Devang Rathod
  • 6,650
  • 2
  • 23
  • 32