无法在AJAX文件中运行javascript

时间:2015-07-02 07:11:03

标签: php mysql

我有一个php文件,我正在从php发送变量到ajax文件,一切都运行得很好,但是当我在ajax文件中运行javascript 没有任何东西看起来像警告一个消息,没有任何东西出现,ajax和javascript之间是否有任何冲突? 这是在ajax文件

<script type="text/javascript" src="js/library.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>

<?php
session_start();

include("mysqlconnection.php");

if (!isset($_SESSION['username'])) {
    echo "<h2 align='center'>Warning: You are not currently signed in </h2>";
    die ("<script type = 'text/javascript' language = 'JavaScript'>window.setTimeout(\"location.href='index.php'\", 1000); </script>");
}
$uid=$_SESSION['uid'];
$code = $_GET['user'];
$qty = $_GET['qty'];
$custid = $_GET['custid'];
$warehouseid = $_GET['warehouseid'];


if ($code==''){
    $sql1 = "SELECT * FROM itemscopy WHERE uid='$uid'";
                $result1=mysql_query($sql1,$con);
                if(!$result1)die("Couldn't execute".mysql_error());
                ?>

    <table border="0" width="100%" align="center"> 
        <thead>
            <tr>
                <th>Code</th>
                <th>Code 2</th>
                <th>Description</th>
                <th>Discount%</th>
                <th>Discount Amount</th>
                <th>Quantity</th>
                <th>Price</th>
                <th>Amount</th>
                <th>After Discount</th>
                <th>Warehouse</th>
            </tr>
        </thead>
<?php
for($counter=0;$row1=mysql_fetch_assoc($result1);$counter++){

                $wid=$row1['warehouseid'];

                $selwarehouse="SELECT * FROM warehouse 
                                WHERE Id='$wid'";
                $runwarehouse=mysql_query($selwarehouse,$con);
                if(!$runwarehouse)die("Error");
                $rowz=mysql_fetch_assoc($runwarehouse);

            if($counter%2==0){
                echo ("<tr class='light'>");
                echo ("<th>".$row1['code']."</th>");
                echo ("<th>".$row1['code2']."</th>");
                echo ("<th>".$row1['description']."</th>");
                echo ("<th>".$row1['discountper']."</th>");
                echo ("<th>".$row1['discountamount']."</th>");
                echo ("<th>".$row1['quantity']."</th>");
                echo ("<th>".$row1['price']."</th>");
                echo ("<th>".$row1['amount']."</th>");
                echo ("<th>".$row1['totaldiscount']."</th>");
                echo ("<th>".$rowz['name']."</th>");
                echo ("<th><a href='edit.php?id=".$row1['Id']."'>edit</a></th>");
                echo ("</tr>");
            }else{
                echo ("<tr class='dark'>");
                echo ("<th>".$row1['code']."</th>");
                echo ("<th>".$row1['code2']."</th>");
                echo ("<th>".$row1['description']."</th>");
                echo ("<th>".$row1['discountper']."</th>");
                echo ("<th>".$row1['discountamount']."</th>");
                echo ("<th>".$row1['quantity']."</th>");
                echo ("<th>".$row1['price']."</th>");
                echo ("<th>".$row1['amount']."</th>");
                echo ("<th>".$row1['totaldiscount']."</th>");
                echo ("<th>".$rowz['name']."</th>");
                echo ("<th><a href='edit.php?id=".$row1['Id']."'>edit</a></th>");
                echo ("</tr>");
        }
    }

            echo "</table>";
?>

        </div>
        </body>
</html>
    <?php
    die('');
}
?>

<head>
</head>
<?php
if($warehouseid==''){
die("<h2 align='center'>Please Choose warehouse</h2>");
}           
                $x=(explode('-',$code));


                $code1=$x[0];
                $codeid=$x[1];
                $code2=$x[2];

                $selqty="SELECT * FROM inventory 
                            WHERE itemid='$codeid' AND 
                                    warehouseid='$warehouseid'";
                $runselqty=mysql_query($selqty,$con);
                $rowinventory=mysql_fetch_assoc($runselqty);
                if(!$runselqty)die("error");
                if(mysql_num_rows($runselqty)==0){

                    die("<h1 align='center'>This Item Is Not In Your Inventory</h1>");

                }
                else{
                    if($rowinventory['qty']<0){
                ?>
                        <script type='text/javascript'>
                    function check(){
                        var r = confirm("Press a button!");

                        if(r) {
                            var urlLink = 'http://www.example.com/warehouse/record.php?codeid=<?php echo $codeid;?>&qty=<?php echo $qty;?>&custid=<?php echo $custid;?>&warehouseid=<?php echo $warehouseid;?>';

                            $.ajax({
                                type: 'GET',
                                url: urlLink,
                                success: function(data) {
                                    if(data == 'success') {
                                        alert('123'); 
                                    }
                                },
                                error: function(data) {
                                    console.log(data);
                                }
                            });
                        } else {
                         return false;   
                        }
                    }
                   check();
                   alert('123');
                </script>

                        <?php
                    //  die("<h1 align='center'>The quantity of the item is negative Do you want to continue ?</h1>");
                    }
                else if($rowinventory['qty']-$qty>=0){
                    //  die("<h1 align='center'>The quantity is enough in the inventory You can continue</h1>");
                    }
                else if($rowinventory['qty']-$qty<0){
                    //  die("<h1 align='center'>The available quantity in the inventory is less than the quantity you select</h1>");
                    }
                }


                    $sqlx = "SELECT * FROM items
                            WHERE Id='$codeid'";

                    $resultx=mysql_query($sqlx,$con);
                    if(!$resultx)die("Couldn't execute".mysql_error());
                    $row = mysql_fetch_assoc($resultx); 

                    $desc=$row['description'];
                    $price=$row['price'];

                    if((!isset($qty))||($qty=='')){

                    $insert="INSERT INTO itemscopy(code,code2,description,quantity,price,amount,custid,uid,discountper,discountamount,totaldiscount,warehouseid,codeid)Values('$code1','$code2','$desc','1','$price','$price','$custid','$uid','0','0','$price','$warehouseid','$codeid')";
                    $r=mysql_query($insert,$con);
                    if(!$r)die("error".mysql_error());
                    }
                else{
                    $insert="INSERT INTO itemscopy(code,code2,description,quantity,price,amount,custid,uid,discountper,discountamount,totaldiscount,warehouseid,codeid)Values('$code1','$code2','$desc','$qty','$price','$price'*'$qty','$custid','$uid','0','0','$price'*'$qty','$warehouseid','$codeid')";
                    $r=mysql_query($insert,$con);
                    if(!$r)die("error".mysql_error());
                }   




                $sql1 = "SELECT * FROM itemscopy WHERE uid='$uid'";
                $result1=mysql_query($sql1,$con);
                if(!$result1)die("Couldn't execute".mysql_error());
                ?>

    <table border="0" width="100%" align="center"> 
        <thead>
        <tr>
        <th>Code</th>
        <th>Code 2</th>
        <th>Description</th>
        <th>Discount%</th>
        <th>Discount Amount</th>
        <th>Quantity</th>
        <th>Price</th>
        <th>Amount</th>
        <th>After Discount</th>
        <th>Warehouse</th>
        </tr>
        </thead>
<?php
for($counter=0;$row1=mysql_fetch_assoc($result1);$counter++){

                $wid=$row1['warehouseid'];

                $selwarehouse="SELECT * FROM warehouse 
                                WHERE Id='$wid'";
                $runwarehouse=mysql_query($selwarehouse,$con);
                if(!$runwarehouse)die("Error");
                $rowz=mysql_fetch_assoc($runwarehouse);

            if($counter%2==0){
                echo ("<tr class='light'>");
                echo ("<th>".$row1['code']."</th>");
                echo ("<th>".$row1['code2']."</th>");
                echo ("<th>".$row1['description']."</th>");
                echo ("<th>".$row1['discountper']."</th>");
                echo ("<th>".$row1['discountamount']."</th>");
                echo ("<th>".$row1['quantity']."</th>");
                echo ("<th>".$row1['price']."</th>");
                echo ("<th>".$row1['amount']."</th>");
                echo ("<th>".$row1['totaldiscount']."</th>");
                echo ("<th>".$rowz['name']."</th>");
                echo ("<th><a href='edit.php?id=".$row1['Id']."'>edit</a></th>");
                echo ("</tr>");
            }else{
                echo ("<tr class='dark'>");
                echo ("<th>".$row1['code']."</th>");
                echo ("<th>".$row1['code2']."</th>");
                echo ("<th>".$row1['description']."</th>");
                echo ("<th>".$row1['discountper']."</th>");
                echo ("<th>".$row1['discountamount']."</th>");
                echo ("<th>".$row1['quantity']."</th>");
                echo ("<th>".$row1['price']."</th>");
                echo ("<th>".$row1['amount']."</th>");
                echo ("<th>".$row1['totaldiscount']."</th>");
                echo ("<th>".$rowz['name']."</th>");
                echo ("<th><a href='edit.php?id=".$row1['Id']."'>edit</a></th>");
                echo ("</tr>");
        }
    }
    echo "</table>";
?>

        </div>
        </body>
</html>

这是我运行ajax文件的php代码,我应该把javascript放在哪里

<?php

include("header.php");
include("mysqlconnection.php");
if(!isset($_POST['custname'])){
die("<h2 align='center'>YOU ARE NOT ALLOWED TO VISIT THIS PAGE</h2>");
}
?>

<html>
<head>
    <script type="text/javascript" src="jquery1.9.1.min.js"></script>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="jquerysearchabledropdown.min.js"></script>
<script>
function showUser(){
var str=document.form.users.value;
var qty=document.form.qty.value;
var custid=document.form.custid.value;
var warehouseid=document.form.warehouse.value;

if (window.XMLHttpRequest){
  xmlhttp=new XMLHttpRequest();
  }

xmlhttp.onreadystatechange=function(){
  if (xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    alert('123');
    }
  }
xmlhttp.open("GET","getcode.php?user="+str+"&qty="+qty+"&custid="+custid+"&warehouseid="+warehouseid,true);
xmlhttp.send();
document.form.users.value='';
document.form.users.focus();
}

$(document).ready(function(){


$("#users").keypress(function(event) {
    if (event.which == 13) {
    event.preventDefault();
        document.getElementById('goo').click();
        }
});
});


        $(document).ready(function() {
            $("#users2").searchable();
        });
        function appendSelectOption(str) {
            $("#users2").append("<option value=\"" + str + "\">" + str + "</option>");
        }

</script>
</head>
<body onload="document.form.users.focus()">
<div id="">
</br></br>
<form name="form" method="post">
<table border="0" align="center">
    <tr>

        <td align="center"><select id="users2" name="users" width="350" style="width: 230px">
                <option value="" selected="selected">Please Select</option>
                <?php

                    $sql1="select code,description,code2,Id from items";
                    $result1=mysql_query($sql1,$con);

                    for($counter=0;$row=mysql_fetch_row($result1);$counter++)
                    print ("<option value = '$row[0]-$row[3]-$row[2]'>$row[0] - $row[1] - $row[2]</option>");
                ?>
        </select>
        </td>
        <td align="center"><select id="warehouse" name="warehouse" >
                <option value="" selected="selected">Please Select</option>
                <?php

                    $sql1="select Id,name from warehouse order by Id Desc";
                    $result1=mysql_query($sql1,$con);

                    for($counter=0;$row=mysql_fetch_row($result1);$counter++)
                    print ("<option value = '$row[0]'>$row[1]</option>");
                ?>
        </select>
        </td>
        <td>
        <input type="text"  name="qty" id="qty" size="2"/>
        </td>
        <td>
        <input type="button" name="goo" id="goo" value="GO" onclick="showUser()"/>
        </td>
        <td>
        <input type="hidden"  name="custid" id="custid" value="<?php echo $_POST['custname'];?>" size="3"/>
        </td>
        <td>
        <input type="hidden"  name="warehouseid" id="warehouseid" value="<?php echo $_POST['warehouse'];?>" size="3"/>
        </td>
        <td>
        <input type="hidden"  name="date" id="date" value="<?php echo $_POST['date'];?>"/>
        </td>
    </tr>
    </table>
    </form>
</div>
<div id="txtHint"></div>
<script>
$(document).ready(function(){
    $('#users').attr("placeholder", "Barcode Search...");

    $('#users').focusin(function(){             
        $(this).attr("placeholder", "");
    });

    $('#users').focusout(function(){                
        $(this).attr("placeholder", "Barcode Search...");
    });

});
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

要在ajax调用后运行javascript函数,您应该执行以下操作

 $.ajax({
            url: "getcode.php?user="+str+"&qty="+qty+"&custid="+custid+"&warehouseid="+warehouseid,
            type:"get",
            success: function(responseText) {
                $("#txtHint").html(responseText);
                $("#txtHint").find("script").each(function(i) {
                    eval($(this).text());
                });
            }
        });

将您的javascript函数放入success函数中.. 因此,当ajax调用成功时,该函数将触发

修改

根据您的编码,您需要在功能中进行更改

xmlhttp.onreadystatechange=function(){
  if (xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    alert('something') // put some javascript here
    }

如果需要任何其他帮助,请告诉我