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 8184879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:39:35+00:00 2026-06-07T01:39:35+00:00

I am attempting to add data to my database from my HTML code via

  • 0

I am attempting to add data to my database from my HTML code via the use of JQuery, AJAX/JSON and PHP using an MVC model. Below is a small sample of what I am looking to achieve.

In my front end I have a checkbox with different options and a button named ‘Add’. The selected elements from here are picked up by a Javascript function, which I have tested properly, once this is done I call another Javascript function to do the AJAX/JSON . What I am still fresh on is the actual AJAX/JSON process that sends the data to PHP.

My Javascript function:

function add_fruits(fruit_name, fruit_type){
    var success = "Fruit added";
    var error = "Fruit not added";

    var params = {
        'fruit_name' : fruit_name,
        'fruit_type' : fruit_type
    };

    $.ajax({
        type: "POST",
        url: "add_fruits.php",
        async: false,
        data: params,
              success: function(success){
                  alert(success);
              },
              error: function(error){
                  alert(error);
              }
    });
}

My PHP function:

<?php
header("Access-Control-Allow-Origin: *");
header('Content-type: application/json');

require_once 'lib/connection_files.php';


if($_SERVER['REQUEST_METHOD'] =='POST')
{
    $fruit_name = no_sql_injection($_POST['fruit_name']);
    $fruit_type = no_sql_injection($_POST['fruit_type']);

    $fruits = new fruits();
    $result = $fruits->add_fruits($fruit_name, $fruit_type);
    $tmp = mysql_num_rows($result);

    if($result == 1)
    {//RESULT must return 1 to verify successful insertion to database
        //send confirmation to front end
    }
    else
    {
        //send error message to front end
    }
}
else{
    //tell front end there was error sending data via AJAX
}
?>

Note that the add_fruits() function takes care of doing the Queries to the database, I did not include it here because it is irrelevant to my issue.

  • 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-06-07T01:39:36+00:00Added an answer on June 7, 2026 at 1:39 am

    Just do echo in your PHP:

    PHP

    else {
        //send error message to front end
        echo "Error Adding Fruits";
    }
    

    JS

    success: function(data) {
        if (data == "1") {
            //data added to db
        }
        else {
            alert(data);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently attempting to retrieve a list of objects from my database using jQuery.
I am attempting to send some data via JSON to a MVC controller action:
I'm currently attempting to use the Windows Powerpack DataRepeater control to display data from
I'm attempting to merge data from one MySQL database into another. The problem is,
I’m attempting to use ADO.NET Data Services to update my database. Tables: Person( PK
In Visual Studio 2010, attempting to use the Server Explorer to add a data
I am attempting to add Entity Framework, code first, to an MVC application that's
I am attempting add some tests to an existing QT GUI application using QTest.
I'm attempting to add a cmake build system to an old fortran code that
I am attempting to retrieve a list of objects from Entity Framework via WCF,

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.