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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:11:10+00:00 2026-06-16T12:11:10+00:00

I have an html page that would send json request to get the data

  • 0

I have an html page that would send json request to get the data from a server [though php-mysql]. I see that the response is not being received. I tried debugging through fiddler and could see that “The response does not contain valid json text.

My code is below

html

<!DOCTYPE html>
<html>
<script src="http://code.jquery.com/jquery-latest.js" 
        type="text/javascript">
</script>

<script type="text/javascript">
function populateFruitVariety() {

    $.getJSON('serverside.php', 
               {fruitName:$('#fruitName').val()}, 
               function(data)  {

    var select = $('#fruitVariety');

    if(select.prop) {
        var options = select.attr('options');
    } else {
        var options = select.attr('options');
    }

    $('option', select).remove();

    $.each(data, function(val, text) {
        options[options.length] = new Option(text, val);
    });
    });
}

$(document).ready(function() {
    populateFruitVariety();
    $('#fruitName').change(function() {
        populateFruitVariety();
    });
});

</script>

<form>
    Fruit:
    <select name="name" id="fruitName">
        <option>Apple</option>
        <option>Banana</option>
        <option>Orange</option>
        <option>Pear</option>
    </select>
    Variety:
    <select name="variety" id="fruitVariety">
    </select>
</form>
</html>

serverside.php

<?php
header('Content-type: application/json');

$host="localhost";
$username="root";
$password="";
$database="db_common";
$table_fruit_info = "fruit_info";
$tc_fruit_id = "fruit_id";
$tc_fruit_index = 'fruit_index';

$con = mysql_connect("$host", "$username", "$password") 
       or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());

if(isset($_GET['fruitName'])) {
    $fruit_id = $_GET['fruitName'];
    $result = mysql_query("SELECT * FROM $table_fruit_info
                WHERE $tc_fruit_id='$fruit_id'");
    while($rows = mysql_fetch_array($result))
    {
        $ret_fruitIndex = $rows[$tc_fruit_index];
    }
}
echo json_encode($ret_fruitIndex);
?>

Thanks for your help!

  • 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-16T12:11:12+00:00Added an answer on June 16, 2026 at 12:11 pm

    It looks like $ret_fruitIndex is just a string, which dosn’t have an appropirate JSON representative. In your while loop, you are overwriting the value for this variable with each pass, which I assume is not what you are wanting to do.

    I assume you really want to so something like:

    $ret_fruitIndex[] = $rows[$tc_fruit_index];
    

    As an side you code is very vulnerable to SQL injection and you also should not be using teh mysql_* functions which are deprecated. I would suggest using mysqli_* functions, PDO, or something more modern and secure.

    With regards to your jQuery: I am not sure what you are doing here:

    if(select.prop) {
        var options = select.attr('options');
    } else {
        var options = select.attr('options');
    }
    

    As there is not any difference in cases there.

    I also believe your each() function is wrong. The parameters passed to the callback function will be the index value of data and the value for that index.

    You logic should be to remove all options, than add new options based on data, so I don;t know why the options variable has any involvement in this callback function at all.

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

Sidebar

Related Questions

I have an HTML page that uses AJAX to retrieve messages from a server.
I have created an Html 5 page that provides important server-side functionality. Unfortunately, it
I have a page that sends a request to the server. On the server
I have a need for a simple html + javascript page that must send
I have a html page that allows users to submit a file. Below is
I have an html page that open a popup window when the page loads.
I have an HTML page that contains some filenames that i want to download
I have an HTML page that roughly looks like this: <div id=rolesRollerBody style=height: 309px;>
Ok, so as the title says, I have an HTML page that I fetch
This is bugging me ... I have a simple HTML page that has a

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.