Php 无法在AJAX文件中运行javascript

Php 无法在AJAX文件中运行javascript,php,mysql,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

我有一个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>

要在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
    }

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

没有什么比ajax文件更好的了-ajax是一个编程概念,是javascript的一部分。我已经展示了我的代码,好的,但是我如何运行javascript alert,或者在ajax中的确认框中,如果我有条件,我需要使用是或否框进行确认将您的警报或任何其他javascript代码放入ajaxSuccess中function@LebaneseInternational您不能在内部编写javascript,如果您正在进行ajax调用,那么您应该在php端回显BLABLA,然后像$.ajax{}那样使用它.successfunctiondata{alertdata};你有什么错误吗??如果它得到了一个成功的ajax调用,那么这应该是可行的。。。检查我的编辑我需要检查ajax文件中的许多条件,并根据我得到的结果发出警报,我不需要在ajax文件运行时发出javascript警报,我需要根据ResultsOK运行它。。然后在ajax响应中返回一些标志。。。像ifresponse==1{alert'1'}或者ifresponse==2{alert'2'}这样的东西仍然不起作用,因为我的ajax文件中没有运行javascript或jquery