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

  • Home
  • SEARCH
  • 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 6190269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:33:25+00:00 2026-05-24T02:33:25+00:00

There seems to be a problem with the code I have for calling php

  • 0

There seems to be a problem with the code I have for calling php from javascript with jquery ajax. The ajax call seems to be successful but I don’t get the correct information returned from the php function.

In the php function I create a SQL query. I send back the query as a reponse to debug it before performing a delete query. Here is the HTML for the div to show the query.

 <div id="thenode" style="position: absolute; top: 30px; left: 0px; width: 150px; background-color: white; z-index: 9999;">&nbsp;</div>

Here is the jquery ajax call. There are two variables being sent to the PHP function: nodeid for node to be delete, and option delete for the function.

function deleteitem()
{

     //get selected node
     var selectnod = getCookie('pnodid'); 

     //define php info and make ajax call
     $.ajax({
         url: "uptree.php",
         type: "POST",
         data: { node: selectnod, option: "delete" },
         cache: false,
         success: function (response) {
             $('#thenode').html(response);
         }
     });

}

Here is the PHP function.

<?php

function uptree() {

  $node = $_POST['node'];
  $option = $_POST['option'];

  if($node == '' || $option == '') {
    return '';
  }

  $dbco = mysql_connect('localhost', 'root', 'mmowebdb');
  if (!$dbco)
    {
    die('Could not connect: ' . mysql_error());
    }

  mysql_select_db("pagelinks", $dbco);

  $sql = "DELETE FROM dtree_table WHERE nid='$node'";

  return $sql;
}

?>

Should be straightforward but this ajax call returns an empty string and causes the div in the HTML to disappear. This is the first time I use ajax in an actual project. The problem must be easy to find for someone who knows what ajax really does. Can you tell the problems?

  • 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-24T02:33:27+00:00Added an answer on May 24, 2026 at 2:33 am

    I found the answer! Thanks to all of you who had suggestions about the SQL call. But here is the actual answer to my question.

    There are four steps in making an ajax Javascript to PHP call. The first two steps happen in the Javascript. The other two steps happen in the PHP.

    Step 1. In Javascript decide what variables are needed in the PHP function, retrieve them.

    Step 2. Make the ajax call to the PHP function. jquery has a convenient way of passing values to PHP. You have a an array of name-value pairs like this in the data item for the ajax call.

     data: { node: selectnod, option: "delete" },
    

    Step 3. Have your PHP function ready in a PHP file. Write the function like this.

    function updatetree($node, $option) {
    

    Step 4. Echo a call to the php function within that PHP file.

    With these four steps you should have a succesful call to PHP and be able to return information to javascript from the PHP function.

    Here is the javascript function.

    function deleteitem()
    {
    
         //Get selected node to send to PHP function
         var selectnod = getCookie('pnodid'); 
    
         //Define php info, specify name of PHP file NOT PHP function
         //Note that by loading the PHP file you will probably execute any code in that file
         //that does not require a function call
         //Send PHP variables in the data item, and make ajax call
         //On success perform any action that you want, such as load a div here called thenode
         $.ajax({
             url: "uptree.php",
             type: "POST",
             data: { node: selectnod, option: "delete" },
             cache: false,
             success: function (response) {
                 $('#thenode').html(response);
             }
         });
    
    }
    

    Here is the PHP file uptree.PHP. It has a function defined, called updatetree. It also has an echo statement to call that function. This just seems to be the way to cause the function to run. Ajax itself doesn’t call the function.

    <?php
    
    //Function defined here
    //The variables will come from the ajax data statement
    function updatetree($node, $option) {
    
      if($node == '' || $option == '') {
        return 'Select an item in the tree.';
      }
    
      $dbco = mysql_connect('localhost', 'root', 'mmowebdb');
      if (!$dbco)
        {
        die('Could not connect: ' . mysql_error());
        }
    
      mysql_select_db("pagelinks", $dbco);
    
      $sql = '';
      switch($option) {
         case 'delete':
            $sql = "DELETE FROM dtree_table WHERE nid='$node'";
            break;
         case 'add':
            list($pagename, $address) = explode(",", $page);
            $pagename = trim($pagename);
            $address = trim($address);
            $sql = "INSERT INTO dtree_table (nid, pid, name, url) values (NULL, ".$node.", '".$pagename."', '".$address."')";
            break;
         case 'update':
            break;
      }
    
      if (!empty($sql)) return $sql;
    }
    
    //echo statement to run function, variables sent by ajax are retrieved with $_REQUEST
    //they could have also been retrieved with $_GET or $_POST
    echo updatetree(trim($_REQUEST['node']),trim($_REQUEST['option']),trim($_REQUEST['page']));
    
    ?>
    

    So to recap. Javascript gets variables, makes ajax call to PHP file. Ajax loads PHP file which contains echo statement that causes PHP function to run. That PHP function is defined in that same file. The function return statement sends information back to javascript through ajax. Javascript does something with that information, e.g. load it into a div on the HTML page.

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

Sidebar

Related Questions

I've created a custom thread pool utility, but there seems to be a problem
So there seems to be this problem with GNU Make's $(wildcard) function keeping a
I've had some experience with Pygame, but there seems to be a lot of
There seems to be no good way to localize a WPF application. MSDN seems
There seems to be a lot of heated discussion on the net about the
There seems to be two major conventions for organizing project files and then many
There seems to be three common approaches for mapping an application end user to
I've noticed that there seems to be quite a bit of hostility towards Linq
Unfortunately, there seems to be no string.Split(string separator), only string.Split(char speparator). I want to
How exactly do DLL files work? There seems to be an awful lot of

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.