Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7699907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:35:18+00:00 2026-05-31T22:35:18+00:00

I am trying some things with JSON and PHP and there is something that

  • 0

I am trying some things with JSON and PHP and there is something that I can’t find a way to do, though I’m not 100% sure there is one. But because it looks like a nice option (If possible) I decided to ask here.

I have these examples from jquery offical site. There are two files, the first one is index.php where I execute my Ajax, hete it is:

<!DOCTYPE html>
<html>
<head>
<title>Simple form sending and receiving a JSON object to/from PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  var data = 
  { 
    "sales": [ 
      { "firstname" : "John", "lastname" : "Brown" },
      { "firstname" : "Marc", "lastname" : "Johnson" }
    ] // end of sales array
  }
  var dataString = JSON.stringify(data);
  $.post('simpleformSubmi.php', { data: dataString}, showResult, "text");
});

function showResult(res)
{
  $("#fullresponse").html("Full response: " +res);
}
</script>
<div id="fullresponse"></div>
</head>
<body>

Nothing complicated at all. And I have my simpleformSubmi.php which is :

<?php

$logFile = 'logFile';
$res = json_decode(stripslashes($_POST['data']), true);
error_log("result: ".$_POST['data'].", res=".json_encode($res), 3, $logFile);
error_log("\n", 3, $logFile);

//header("Content-type: text/plain");
foreach ($res as $key=>$value)
{
    $str[] = $value;
}
$functionArray ="function(){ \$a = 10; echo \$a;}";
$jsStr = $str[0][1];
echo json_encode($jsStr['firstname']);
echo '<hr />';
echo json_encode($res);
echo '<hr />';
echo json_encode($functionArray);
?>

As you can see $functionArray – is in fact a string containing PHP function which I want to return back using JSON and to execute it after that. So is there any way to do that really? Now what I get in index.php afet executing the files is:

"function(){ $a = 10; echo $a;}“

Thanks
Lern

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:35:20+00:00Added an answer on May 31, 2026 at 10:35 pm

    Seems like you’re trying to execute a PHP function through JavaScript. Since PHP is executed server-side the only way you have to execute a PHP function in that context is to ask the server back to execute the function for you, by doing another ajax call for example.

    Something like this:

    index.php

    $(document).ready(function(){ 
      var data = 
      { 
        "sales": [ 
          { "firstname" : "John", "lastname" : "Brown" },
          { "firstname" : "Marc", "lastname" : "Johnson" }
        ] // end of sales array
      }
      var dataString = JSON.stringify(data);
    
      //Change post() success callback function to executePHP()
      $.post('simpleformSubmi.php', { data: dataString}, executePHP, "text");
    
      //Let's define executePHP() outside post() call for clarity
      function executePHP()
      {
          //Ask the server to execute function foo(), and get the result
          $.get("example.com/somefile.php?function=foo", function(data)
          {
              //Success function, do whatever you want.
              alert(data);
          });
      }
    });
    

    Then, in somefile.php

    <?php
    //Condition(s), if any. You could even implement this interface using REST.
    //Get function to execute
    if($_GET["function"] == "foo")
    {
        //Output function's result.
        echo foo();
    }
    
    //The function you want to run
    function foo()
    {
        //Do something
        $a = 10;
        return $a;
    }
    ?>
    

    If all went well, when JavaScript reaches the alert(data); statement you will see 10.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying something very simple, but for some reason it does not work.
I'm trying to build a PHP form that outputs custom JSON code. Take a
I'm trying to do something like so: $([type=text]).autocomplete({ source: ./json.php?id=+$(this).attr(id), minLength: 2 }).addClass(ui-widget ui-widget-content
I'm trying to do multiple things in curl and obviously some parameters in each
Hello I'm trying something .. I was reading Jquery traversing documentations some forums, this
I'm trying something like this, but this example does not work. jsObj = {};
i have a script that calls a php file and gets an JSON object
I am trying to parse the JSON returned here: http://www.waterwayguide.com/map/php/bridge.php?ll=25.514339,-80.076164 However, I can't parse
I'm trying to do something that goes far over my current skills yet I
I have this bit of javascript: var jsonString = some string of json; $.post('proxy.php',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.