0

I am using ajax page calling after success call a php function but issue is that function is not defined error show in console panel.

here is the ajax code

$("#submit_btn").on('click',function() {
var batchid = $('#batchid').val();
var sessionid = $('#sessionid').val();
var moduleid = $('#moduleid').val();
var instructorid = $('#instructorid').val();
    $.ajax({
    type: "POST",
    url: "check_lp.php",
    data: {batch: batchid, session: sessionid, module: moduleid, instructor: instructorid},
    success: function(result) {
        var value = $.trim(result);
        if (value == "1"){
            alert('Lesson Plan Found');
        } else{
            submitForm();
        }

    },

});
});

php function code is

function submitForm()
 { 
  }

is there right way to call a php function?

  • Can you please add the contents of your `check_lp.php` ? – jrswgtr Sep 10 '18 at 11:15
  • 4
    If I understand correctly, you would have to make Another AJAX call in order to run another piece of PHP code from there. Remember, PHP code only runs in the Server and not in the Browser – RiggsFolly Sep 10 '18 at 11:15
  • 2
    related https://stackoverflow.com/questions/15757750/how-can-i-call-php-functions-by-javascript – Madhawa Priyashantha Sep 10 '18 at 11:17
  • @jrswgtr is there no issue in this file check_lp.php.. I only know that how to add php code in this format? – Arslan Kiyani Sep 10 '18 at 11:17
  • 1
    You need to fire an Ajax call again, You can't call the PHP function from the JS file. – Ash0ur Sep 10 '18 at 11:19
  • _Additional Suggestion:_ You should use `json_encode()` to return data from the PHP Code to the Javascipt code. Then it becomes a javascript object automatically rather than doing what I assume you are doing which is `echo $var;` So something like `echo json_encode(['status'=>1]);` – RiggsFolly Sep 10 '18 at 11:20

1 Answers1

0

PHP is server-side, it doesn't run in the browser. Your "AJAX" code is jQuery, which is client-side (this is why you need AJAX)

You have two options:

  1. Create a javascript method to act as a wrapper, and use another ajax call inside it:
function submitForm() {
    $.ajax({
        type: "POST",
        url: "myPhpFile.php"
        data: {callSubmitFunction: 1}
        success: function(response) {
            // do something if you need to here
        }
    });
}  
  1. (Assuming your PHP is submitting a form based on the method name) Submit the form using javascript/jQuery instead.
document.getElementById("myForm").submit();
// or
$("#myForm").submit();

Please note the examples given aren't "copy & paste" examples - you will need to amend them to suit your code

SierraOscar
  • 17,507
  • 6
  • 40
  • 68
  • thanks for the supporting but little bit issue is that i write this $("#myForm").submit(); inside in success: function(response) { } then form is not submit – Arslan Kiyani Sep 10 '18 at 11:38
  • did you change `#myForm` to the _actual_ ID of your form? – SierraOscar Sep 10 '18 at 11:44
  • how to get values in php? can you write some php lines like this $batches = $_REQUEST['batchid']; $sessionid = $_REQUEST['sessionid']; $moduleid = $_REQUEST['moduleid']; $weekid = $_REQUEST['weekid']; – Arslan Kiyani Sep 10 '18 at 11:47