0

Hi Guys any help on this? I have a dialog box triggered by a button click that calls my php script to generate a form which needs to be filled and submitted. I want to do the sending and conformation via Ajax. I have been recoding and researching for a few days but nothing on StackExchange or other websites help me with it. Here's the code:

Dialog box snippet;

$k('#CreateTable').click(function(e){
          e.preventDefault();
         var Call =  $k('#CreateTable').attr('value');//.attr('id');

         var util = $k(this).attr('id');//.attr('id');

         $k('#dialog').dialog({
             autoOpen: false,
            title: 'Running Utility for: '+Call,
            modal: true,
            width: 450,
            close: function(event, ui) {
             $k("#dialog").dialog('destroy');//event.target
            }//END CLOSE

        }).dialog('open');
        var utility = { 'utility' : util };
        $k.ajax({
                  type: "post",
                  url: "inc/runUtilities.php",
                  dataType: "html",
                  data: utility,
                  success: function(data) {
                        $k('#DlgTxt').html(data).fadeIn('slow');
                  }
        });
            //return false;
        });//END DIALOG

The PHP snippet;

$show .=  "<form id='cContact' name='cContact' method='post'>";
    // action='".$_SERVER['REQUEST_URI']."'
    $show .= '<table align="center" width="425" border="0">
       ';

        $query = "SHOW COLUMNS FROM `".$_SESSION['WorkTable']."`"; 
        if($output = mysqli_query($this->MySQLCxn->MySQLCxn, $query))
        {
            $columns = array();
            while($row = mysqli_fetch_assoc($output))
            {
                if($row['Field'] == 'id') {}
                else
               $show .= '
                <tr>
                  <td width="175"><div align="right">'.@$row['Field'].':</div></td>
                  <td width="155">
                    <input type="text" name="'.@$row['Field'].'" placeholder="'.@$row['Field'].'" />
                  </td>
                  <td width="115">&nbsp;</td>
                </tr>
                ';
            }
        }


    $show .= '
        <tr>
          <td>Submit </td><td>

          <button type="button" id="cContactSbmt" onclick="doSubmitForm(this); return false;" name="cContactSbmt" value="cContactSbmt">Create contact</button>


          <!-- <input type="submit" class="button" value="Save" name="submit"> -->
          </td> <td>&nbsp;</td>
        </tr>
      </table></form>
      <div id="thanks">

      </div>
       ';

And the JQuery that i am currently using trying to have it call my php to process the form being sent.

var $j = jQuery.noConflict(); 

(function($j){
 $j(document).ready(function() {
    $j("#cContactSbmt").click(function(e){//'click',
         e.preventDefault();
             alert('cContactSbmt clicked...');
         $j.ajax ({
          type:"POST",
          url:"inc/runUtilities.php",
          data: $j(this).serialize(),
          success: function(msg){
            $j("#thanks").html(msg)
            $j(this).modal('hide');
          },
          error: function(){
            alert("failure");
          }
        });
      });


});

})($j);

For some reason its not working nothing showing up in the console as well. again: i have a dialog box that gets populated via Ajax with a php generated from that needs to get submitted to another php script that is to process it and reply to the dialogs. Any suggestions?

3 Answers 3

2

your php ist not outputting ("echoing") anything. you need to echo whatever you want to return to your ajax-call.

update: plus for debugging try to log the returned data in every success callback, for example:

success: function(data) {
    console.log(data);
}
Sign up to request clarification or add additional context in comments.

6 Comments

i would bet money that he returns the $show somewhere...also your "answer" would be more like a comment.
@TopQuestions maybe - but I've seen this exact mistake by many people that are new to ajax.
@northkilidonan wasn't meant to be rude sorry, reading it again sound very rude :/
@TopQuestions well, if it's the solution of his problem - it's an answer. if it isn't, it still is. lol.
@GeraldoIsaaks then please provide your full code and try to debug your ajax callbacks as i mentioned in my answer.
|
0

You may need to add an echo to the end of your php snippet..

echo $show;

This echoed html will then be available in your ajax function in the success callback function... as the variable "data"

success: function(data) {
  $k('#DlgTxt').html(data).fadeIn('slow');
}

Also you may need to use jQuery .on() in your click function..to submit the form..

http://api.jquery.com/on/

$j("body").on("click", "#cContactSbmt", function(){

});

Hope this helps....

1 Comment

Guys i have spent about 2 days just on this code please have a good look at it - i'm only about a year with JQuery so i may be missing some. as to the php code - echoing - the code is a snippet from a class that does quite a lot so i left out the other code...
0

Hi Guys so I got it working with the following changes: On the form;

<button id="cContactSbmt" onClick="doContactsCreate(\'#cContact\')" type="button" class="form-submit"  name="cContactSbmt" value="cContactSbmt">Create contact</button>

I added the onclick method with a callback to a function doContactsCreate(\'#cContact\'), which has the id of the form passed to it.

function doContactsCreate(obj)
{
     alert(obj+': cContactSbmt clicked...');
    /* */
    var frmData = $k(obj).serialize();
         $k.ajax ({
          type:"POST",
          url:"inc/runUtilities.php",
          data: frmData,
          success: function(data){
          $k("#thanks").html(data);
          console.log(data);
            $k(obj).modal('hide');
          },
          error: function(){
            alert("failure");
          }
        });

}   

I've just enclosed the JQuery code in this function definition and placed it outside the

(function($k){
 $k(document).ready(function() {
//rest of JQuery code
})($k);

Don't know why but i think that was the main problem

Comments

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.