简体   繁体   中英

Limited success with AJAX. Sometimes nothing is returned or an error

So far I'm having limited success with AJAX. I have a couple of textboxes I want to fill with values from the database, only nothing is being inserted. Something is happening though which I guess is a good sign, but they're specific to 2 values and they're errors. Nothing seems to be out of the ordinary about these values either. The error report says: Parse error: syntax error, unexpected '}' in getemp.php, but everything lines up correctly and it never happens anywhere else in the database but these values that are back to back. Also since I'm using a table with listboxes no matter which box I choose from it only affects one entry into the table, however I thought the loop would take care of that.

So here's the code I'm trying to use AJAX on:

<? require_once("connect_to_DB.php");  // inserts contents of this file here  ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>New Order Form/Edit Order Form</title>
<link rel="stylesheet" href="hw2.css"/>
<?
connectDB();
session_start();?>
<script src="validation.js"></script>
<script src="jquery.js"></script>
<script type="text/javascript">
function showUser(str){
    if(str==""){
        $("#txtHint").html("");
        return;
    }else{
        $("#txtHint").load("getemp.php?q="+str);
    };
}
</script>

</head>
<body>

<?
    include ('navbar_func.php');
    echo navbar();

// Establish a connection with the data source, and define the SQL

$strSQL = "SELECT product_name FROM product";
$rs = mysqli_query($db, $strSQL)  or die("Error in SQL statement: " . mysqli_error());  
$row = mysqli_fetch_array($rs);

// Establish a connection with the data source, and define the SQL for the orders

$newID = 101;
$rndSQL = "Select order_id FROM salesorder WHERE order_id = ".$newID;
while(mysqli_num_rows(mysqli_query($db, $rndSQL)) > 0){
    echo "<script>console.log($newID)</script>";
    $newID++;
    $rndSQL = "Select order_id FROM salesorder WHERE order_id =". $newID;
}
?>
<?$today = date("F j, Y");?>
<form name="orderform" method="post" action="new_order_result.php" onsubmit="return validate_order()">
    <table>
        <tr>
            <td>Order Number:</td>
            <td><label id="order" type="text" name="ordernumber" value="<?=$newID?>"><?=$newID?></label>
            </td>
            <td>Order Date:</td>
            <td><label type="text" name="orderdate" value="<?=$today?>"/><?=$today?></label></td>
        </tr>
        <tr>
            <td> Customer:</td>
            <td><input id="customer"type="text" name="customer" value=""/></td>
        </tr>
        <tr>
            <td>Sale Agent:</td>
            <td><input id="salesagent" type="text" name="salesagent" value=""/></td>
            <td>Order Status:</td>
            <td><input id="orderstatus" type="text" name="orderstatus" value=""/></td>
        </tr>
    </table>
    <table border = "1">
        <tr>
            <th>Product</th>
            <th>Price</th>
            <th>Quantity</th>
        </tr>
        <?$rs = mysqli_query($db, $strSQL)  or die("Error in SQL statement: " . mysqli_error());?>
        <?for($x=0; $x <= 19; $x++)
        //just needs to post quantity not which
            {?>
                <tr>
                <td>
                <select name="P<?=$x?>" onchange="showUser(this.value)">
                    <option value="">Choose the product you'd like to purchase:</option>
                    <?while($row = mysqli_fetch_array($rs)){?>
                    <?print '<option value="'.$row[0].'">' . $row[0] . '</option>' . "\n";}//This is uses the datebase values?>
                </select>
                </td>
                <td>
                <div id="txtHint"><input type="text" name="M<?=$x?>" value="0"></input></div>
                </td>
                <td><select name="Q<?=$x?>"  value="$row[1]">
                        <?for($i = 0; $i < 10; $i++){
                        print "<option value=$i>$i</option>";}//This uses the datebase values?>
                        </select></td>
                </tr>

                <? $rs = mysqli_query($db, $strSQL); //resets pointer in database.?>
          <?}?>
    </table> 
    <center>
        <input type="submit" value="Submit"/>
        <input type="reset" value="Reset"/>
    </center>
</form>
</body>
</html>

Here's getemp.php which is being used in the script.

<? require_once("connect_to_DB.php");  // connect to furniture database

// ###################### retrieve data from database #################
connectDB();  
$sql = "SELECT * FROM product WHERE product_name = " . $_GET["q"];
$result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error());  
// ###############################################################


while($row = mysqli_fetch_array($result))
{
print $row['product_cost']
}

mysqli_close($db);
?>

As of right now the only way I can test for a reaction is by having "div" tags surrounding the textbox.

Thank you so much for the help or tips. I've spent so many hours on just this small thing and I can't make much more progress more than this. I've tried everything and looked everywhere for possible solutions, but none seemed to work. Thanks again!

In getemp.php:

print $row['product_cost'] is missing a semicolon.

Should be:

print $row['product_cost'];

Which is why you're seeing the error Parse error: syntax error, unexpected '}' in getemp.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM