0

I have checked around, but can't seem to figure out how this is done. I would like to send form data to PHP to have it processed and inserted into a database (this is working).

Then I would like to send a variable ($selected_moid) back from PHP to a JavaScript function (the same one if possible) so that it can be used again.

function submit_data() {
    "use strict";
    $.post('insert.php', $('#formName').formSerialize());
    $.get('add_host.cgi?moid='.$selected_moid.');
}

Here is my latest attempt, but still getting errors:

PHP:

$get_moid = "
    SELECT ID FROM nagios.view_all_monitored_objects
    WHERE CoID='$company'
    AND MoTypeID='$type'
    AND MoName='$name'
    AND DNS='$name.$selected_shortname.mon'
    AND IP='$ip'
    ";
while($MonitoredObjectID = mysql_fetch_row($get_moid)){
    //Sets MonitoredObjectID for added/edited device.
    $Response = $MonitoredObjectID;
    if ($logon_choice = '1') {
        $Response = $Response'&'$logon_id;
        $Response = $Response'&'$logon_pwd;
    }
}
echo json_encode($response);

JS:

function submit_data(action, formName) {
    "use strict";
    $.ajax({
        cache: false,
        type: 'POST',
        url: 'library/plugins/' + action + '.php',
        data: $('#' + formName).serialize(),
        success: function (response) {
            // PROCESS DATA HERE
            var resp = $.parseJSON(response);
            $.get('/nagios/cgi-bin/add_host.cgi', {moid: resp });
            alert('success!');
        },
        error: function (response) {
            //PROCESS HERE FOR FAILURE
            alert('failure 'response);
        }
    });
}
2
  • 1
    stackoverflow.com/questions/17752001/… it is called ajax request Commented Jul 19, 2013 at 23:52
  • There's no need for your $.get() here - you can return the variable as a response from your $.post() and handle it in a callback function. Look at the jQuery doc Commented Jul 19, 2013 at 23:54

3 Answers 3

1

I am going out on a limb on this since your question is not 100% clear. First of all, Javascript AJAX calls are asynchronous, meaning both the $.get and $.post will be call almost simultaneously.

If you are trying to get the response from one and using it in a second call, then you need to nest them in the success function. Since you are using jQuery, take a look at their API to see the arguments your AJAX call can handle (http://api.jquery.com/jQuery.post/)

$.post('insert.php', $('#formName').formSerialize(),function(data){
    $.get('add_host.cgi?moid='+data);
});

In your PHP script, after you have updated the database and everything, just echo the data want. Javascript will take the text and put it in the data variable in the success function.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use a callback function to get the returned value.

function submit_data(action, formName) {
    "use strict";
    $.post('insert.php', $('#' + formName).formSerialize(), function (selected_moid) {
        $.get('add_host.cgi', {moid: selected_moid });
    });
}

5 Comments

Looks promising, I'll try it.
I fixed it to use the formName parameter properly. I'm not sure how you intended action to be used.
I actually meant to take that out for simplicity's sake. The parameters were only used in the real JS. For use on here, I didn't think it was needed to have them present. I updated the OP with the more simplified example.
They don't affect the important part of the answer.
I thought I chose this one, but just saw that I never did. Sorry. This worked for me!
0
    $("ID OF THE SUBMIT BUTTON").click(function() {
            $.ajax({
                cache: false,
                type: 'POST',
                url: 'FILE IN HERE FOR PROCESSING',
                data: $("ID HERE OF THE FORM").serialize(),
                success: function(data) {
                   // PROCESS DATA HERE

                },
                error: function(data) {
                   //PROCESS HERE FOR FAILURE
                }
            });
            return false; //This stops the Button from Actually Preforming
        });

Now for the Php

<?php
 start_session(); <-- This will make it share the same Session Princables
 //error check and soforth use $_POST[] to get everything
$Response = array('success'=>true, 'VAR'=>'DATA'); <--- Success 
$Response = array('success'=>false, 'VAR'=>'DATA'); <--- fails
  echo json_encode($Response);
?>

I forgot to Mention, this is using JavaScript/jQuery, and ajax to do this.

Example of this as a Function

    Var Form_Data = THIS IS THE DATA OF THE FORM;
function YOUR FUNCTION HERE(VARS HERE) {
                   $.ajax({
                        cache: false,
                        type: 'POST',
                        url: 'FILE IN HERE FOR PROCESSING',
                        data:Form_Data.serialize(),
                        success: function(data) {
                           // PROCESS DATA HERE

                        },
                        error: function(data) {
                           //PROCESS HERE FOR FAILURE
                        }
                    });
    }

Now you could use this as the Button Click which would also function :3

2 Comments

Can an AJAX function like this be stored in the format I have in the OP? It is called from more than one place would like to still use the function submit_data(); format.
Attempted this, but getting an error in console: Uncaught SyntaxError: Unexpected token P jquery-latest.js:550 jQuery.extend.parseJSON jquery-latest.js:550 $.ajax.success javascript.js:176 fire jquery-latest.js:3048 self.fireWith jquery-latest.js:3160 done jquery-latest.js:8235 callback

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.