2

In my index.php file, I have a php function "InsertTheRecord" which gets a parameter and insert it into the database. It return 1 if that parameter is inserted successfully otherwise 0.

I have following JavaScript function "InsertRecord" in same index.php from where I want to call php InsertTheRecord function. How can I call php function from JavaScript function?

My JavaScript function:

function InsertRecord() {
    var myParameter = 40;
    var result = ////call InsertTheRecord(myParameter) //I don't know how to do this?
    if result == 1 { //do something}
        else { //show error message}
}
Tushar Gupta - curioustushar
  • 56,454
  • 22
  • 99
  • 107

5 Answers5

3

Try

var result = <?php echo InsertTheRecord(myParameter); ?>


updated after OP's comment

$.ajax

function InsertRecord() {
    $.ajax({
        type: "POST",
        url: "your_php_page.php",
        data: "arg1=id&arg2=name",
        success: function (data) {
            var myParameter = 40;
            var result = data;
            if result == 1 {} else {}
        }
    });
}
Tushar Gupta - curioustushar
  • 56,454
  • 22
  • 99
  • 107
3

php server side scripting language and javascript is client side scripting language you can do this by ajax call(Jquery)

<div id="yourdiv"></div>

var data ="hello world";
var data2="hello all";
function run(){
$.ajax({ url: 'myscript.php',
         data: {'q': data,'z':data2},
         type: 'post',
         success: function(output) {
                     alert(output);
   document.getElementById("yourdiv").innerHTML += output; //add output to div  
            }
});
}

myscript.php

   <?php
myfun();

function myfun(){
$myvar2 = $_POST['z'];
$myvar = $_POST['q']."how are you?";
echo $myvar."\n";
echo $myvar2;
}
?>

this alert "hello world how are you?"

user2511140
  • 1,560
  • 2
  • 25
  • 31
2

PHP is serverside, JS is clientside, So the PHP works first, after that the JS works dynamically in the browser.

You cannot call the PHP function in runtime. But you can use AJAX which can do that. Check this out: http://www.w3schools.com/ajax/ajax_aspphp.asp

Esqarrouth
  • 36,821
  • 20
  • 155
  • 161
0

It is not possible

Javascript is client side scripting language where PHP is server side scripting language But, you can try AJAX methods to get similar results where you can pass variables same as function

as function myfunction(var1, var2 ,....){return var1*var2} using ajax, you can run a php script externally on button click $.ajax({key:var},function(result){alert(result)});

http://www.w3schools.com/jquery/jquery_ajax_intro.asp

http://www.tutorialspoint.com/ajax/

Uday Hiwarale
  • 3,788
  • 5
  • 43
  • 45
0

What if you echod the myParameter to a hidden input field and then grabbed it with javascript:

HTML/PHP file:

<input type="hidden" id="myParameter" value="<?php echo InsertTheRecord(myParameter); ?>"/>

In Javascript:

function InsertRecord() {
    var myParameter = 40;
    var result = document.getElementById("myParameter").value();
    if result == 1 { //do something}
    else { //show error message}
}
w3bMak3r
  • 872
  • 8
  • 11
  • I am preparing myParameter inside javascript function after that I want to call PHP function. –  Nov 09 '13 at 10:50
  • I think you should use ajax to call the php function. As said about... The Server parses the PHP first then it is done... then the client runs the Javascript. – w3bMak3r Nov 09 '13 at 10:52