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
  • 58,085
  • 24
  • 103
  • 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
  • 58,085
  • 24
  • 103
  • 107
  • 1
    I personally think you should try to find a solution that keeps the php and javascript separate. I think this method above can lead to trouble as you your project becomes more and more complex. – w3bMak3r Nov 09 '13 at 10:42
  • 1
    @TusharGupta - So should I use $_POST["arg1"] to capture myParameter in my index.php InsertTheRecord function? –  Nov 09 '13 at 10:53
  • @nkp he is using post to send the data. This is a lot better of a solution. I upvoted this one now. – w3bMak3r Nov 09 '13 at 10:54
  • @nkp you can give any name.Example if `data: "abc=id"` than `$_POST['abc']`. – Tushar Gupta - curioustushar Nov 09 '13 at 10:55
  • @nkp process and store the value you are getting with php... then read it in with javascript then use ajax and send the value via post to the php function you want – w3bMak3r Nov 09 '13 at 10:56
  • @TusharGupta - I think there is syntax error in statement data: "arg1=id&arg2=name", -- user2511140 solution is working for me. –  Nov 09 '13 at 11:23
  • @nkp Good that a solution works for you but no syntax error. – Tushar Gupta - curioustushar Nov 09 '13 at 11:25
  • @TusharGupta - Thanks for help Tushar. My problem is resolved now. Upvoted your answer. –  Nov 09 '13 at 12:18
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,658
  • 3
  • 26
  • 32
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
  • 38,543
  • 21
  • 161
  • 168
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
  • 4,028
  • 6
  • 45
  • 48
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
  • 882
  • 8
  • 13
  • 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