-3

I cant make it work. I have a php script on a website (not so good in php, so if something is wrong, please point me). And i want to call it from a js document.

<?php
    function doit($option){
        if ('getit' == $option){
            $value = '318273918739182739179';
            return $value;
        }else{
            return 0;
        }
    }
?>

and i want to call it from a js file. How do i pass an argument to the php script via Ajax?

var getanswer ={
php: function(){
    $.ajax({
           url:'mywebsite.com/php/return.php',
           data: {action, 'doit'},
           type: 'post',
           success: function(output){
           alert(output);
           }
           })
}
}
6
  • Give us more information. What did you try in JS? Commented Feb 14, 2013 at 11:46
  • possible duplicate of Run PHP code inside JavaScript? Would this be ok to use?. Also: Using JQuery Ajax to call a php function Commented Feb 14, 2013 at 11:46
  • sorry, added what i tested in js. Commented Feb 14, 2013 at 11:50
  • I hate how people on stackoverflow make you feel like a complete crap for asking for help. No, i dont want to run php inside js. I am aware that it s not possible. Sorry Commented Feb 14, 2013 at 11:52
  • @ghaschel, maybe you should search around a bit before asking how to use AJAX. There are plenty of tutorials online, and we've seen this question asked more than enough already. Heck, you're already passing data to your php, so there isn't even really a question: data: {action, 'doit'}, Commented Feb 14, 2013 at 11:58

2 Answers 2

0

in javascript:

$.get('yourPHPscript.php?argument='+some_arg,function(data){
 //data contain result from php script
});

in yourPHPscript:

$option = $_GET['argument'];
if ('getit' == $option){
        $value = '318273918739182739179';
        echo $value;
    }else{
        echo "0";
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You can use ajax to call php whenever you need it

for example I have used it for calling a function glob in php

here is some of the jquery code

$('#search_query').keyup(function() {
    var search_input = $("#search_query");
    var output = search_input.val();
    if(output.length > 0){
    $('#app_menu').hide();
    $('#note').show(1000);
        $.ajax({
                type: "POST",
                url: "search_node.php",
                data:{item:output}
            }).done(function( msg ) {
                if(msg!='error')
                {
                    $("div#note").html(msg);
                }
                else
                {

                    $("#debug").append("<br />refresh function failed: ");
                    $("#debug").scrollTop("30");
                }
            });//ajax 

        }
        if  (output.length  == 0){
        $('#note').hide();
        $('#app_menu').show(250);

        }
    });

});

There are some extra stuf in this code that you might not need, hope this helps

2 Comments

what if you have more than 1 function inside the script? How and where do you specify the function you want to call?
@ghaschel do you mean js script or php

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.