0

on clientside(js) im using this script

    jQuery.ajax({
    type: "GET",
    url: "editold.php",
    data: "call=generateCode",
    success: function (response) {
        alert("Succesful getting php file");
    },
    error: function (response) {
         alert("Error getting php file");
    }
});

on serverside(php) im using this code:

<?php 



if($_SERVER['REQUEST_METHOD']=="GET") {
echo '<script type="text/javascript">' . 'alert(" ' . 'hey' . '");' . '</script>'; 
$function = $_GET['call'];

    if(function_exists($function)) {        
        call_user_func($function);
    } 
    else {
        echo 'Function Not Exists!!';
    }
}

function generateCode() {
echo '<script type="text/javascript">' . 'alert(" ' . 'hey' . '");' . '</script>'; 
}
?>    

Although I do get an alert saying my success function alert meaning the file was loaded succesfully I dont seem to be getting the final generatecode function to work.(I dont get a second alert.)

i've tried lots of things but nothing seems to work.

What do i do?..

2
  • 1
    I think your data sent from the client should be an object, and not "call=generateCode". After further thought, it doesn't need to be an object, but I still question your implementation. Commented Sep 22, 2013 at 23:15
  • You don't actually do anything with the response, so your code is behaving as it should. The response is not evaluated, and it isn't executed, so you'd never see a second alert, or even a third. Commented Sep 22, 2013 at 23:16

2 Answers 2

2

This should do it:

jQuery.ajax({
    type: "GET",
    url: "editold.php",
    data: {call: "generateCode"}, // <-- This one is changed into an object
    success: function (response) {
        alert("Succesful getting php file");
        $('body').append(response); // <-- Append the ajax response to the page body
    },
    error: function (response) {
        alert("Error getting php file");
    }
});

If not, try doing var_dump($_GET); in your PHP file.

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

7 Comments

Nothing is ever done with the response, so how would this change what's happening?
It should be '"call"' with quotes, not 'call'!
@Daedalus Thank you! I've added a little append function to the success callback.
@siluaty Quotes are needed in JSON, not JS.
And the data not being in object form is not the problem. It's always good to read the documentation before making a claim like that.
|
0

1-At server side send the response without script tag.

2-At client side use eval() function to execute your code.

if($_SERVER['REQUEST_METHOD']=="GET") {
echo 'alert(" ' . 'hey' . '");';
$function = $_GET['call'];

if(function_exists($function)) {        
    call_user_func($function);
} 
else {
    echo 'Function Not Exists!!';
}
}

function generateCode() {
//your code 
}


jQuery.ajax({
    type: "GET",
    url: "editold.php",
    data: "call=generateCode",
    success: function (response) {
        alert("Succesful getting php file");

        eval(response);
    },
    error: function (response) {
        alert("Error getting php file");
    }
});

1 Comment

Hey thanks alot Shomz it solved my primary problem. but now I have a new little problem :( if you could please help me with this one to.. Now when i change the alert into the code I actually wanted to execute : $db = ConnectToSQLAndGetDBConnSTRVar(); $NewQuery = "UPDATE subtopics SET SubTopic_Name='asfasfa' WHERE SubTopic_ID='spirituality'"; $db->query($NewQuery); it suddenly wont load the php file it gives me the alert of the error function "Error getting php file"

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.