0

我是 php 的新手,还在学习 javascript 函数。

我有一个问题,将 javascript 变量包含到 php 查询中。

我想根据不同类型的零件代码的数量创建交易代码。那么我必须通过计数获得价值。

<script language="javascript" type="text/javascript">
    function getCode(){
    var v =document.forms["form1"]["part_code"].value;
    <?php 
    $query = mysql_query("SELECT COUNT(*)+1 as count FROM TB_TRANSACTION where part_code = "<script>v;</script>"");
    $i=-1;
    while ($code_part = mysql_fetch_array($query))
    {
       $i++;
       ?>
       var getPartCode = <? echo $code_part['count'];?>;
       var x = document.forms["form1"]["part_code"].value;
       var y = document.forms["form1"]["location_code"].value;
       var z = document.forms["form1"]["date"].value;
       var a = (getPartCode + '/'+x +'/'+ y +'/'+ z);
       document.forms["form1"]["invent_code"].value = a;
       <?
    }
    ?>
</script>

结果是这样的1/CPU/JKT/2013

button onClick="getCode()"我在没有提交的情况下调用该函数。

任何人都可以帮助我。

4

2 回答 2

0

最好的方法是Ajax

否则使用 Cookie

例如:

<script type="text/javascript">
    document.cookie = "cookieName=cookieValue";
</script>

<?php 
   $phpVar =  $_COOKIE['cookieName'];
   echo $phpVar;
?>

你的代码

<script language="javascript" type="text/javascript">
    function getCode(){
    var v =document.forms["form1"]["part_code"].value;
     document.cookie = "cookieName="+v;
    <?php 
   $phpVar =  $_COOKIE['cookieName'];
    $query = mysql_query("SELECT COUNT(*)+1 as count FROM TB_TRANSACTION where part_code = '$phpVar'");
    $i=-1;
    while ($code_part = mysql_fetch_array($query))
    {
    $i++;
    ?>
    var getPartCode = <? echo $code_part['count'];?>;
    var x = document.forms["form1"]["part_code"].value;
    var y = document.forms["form1"]["location_code"].value;
    var z = document.forms["form1"]["date"].value;
    var a = (getPartCode + '/'+x +'/'+ y +'/'+ z);
    document.forms["form1"]["invent_code"].value = a;
    <?
    }
    ?>
</script>

更新

检查这个

function getCode(){
var v = 12;
document.cookie = "cookieName="+v;
var getPartCode = <?php echo $_COOKIE['cookieName']; ?>
alert(getPartCode);
}
于 2013-08-21T10:01:37.273 回答
0

你不能用你尝试的方式来做。有两种更好的方法来做

  1. 使用Ajax.

  2. 使用 Cookie

    这里最好的方法是使用 Ajax。在这里,我正在使用javascriptajax。我正在创建一个新文件来创建响应(比如 newfile.php)试试这个,

    function getCode(){
            var v =document.forms["form1"]["part_code"].value;
            var x = document.forms["form1"]["part_code"].value;
            var y = document.forms["form1"]["location_code"].value;
            var z = document.forms["form1"]["date"].value;
            var xmlhttp;
    if (window.XMLHttpRequest)
      {  // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {   // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        var getPartCode=xmlhttp.responseText;  //Getting the response from php file
        var a = (getPartCode + '/'+x +'/'+ y +'/'+ z); 
        document.forms["form1"]["invent_code"].value = a;
        }
      }
    xmlhttp.open("GET","newFile.php?q="+v,true);  //Sending request to newfile.php 
    xmlhttp.send();
            }
    </script>
    

新文件.php

<?php
$value = $_GET["q"];  //getting the value sent through tthe ajax request
$query = mysql_query("SELECT COUNT(*)+1 as count FROM TB_TRANSACTION where part_code = '$value'");
   $i=-1;
    while ($code_part = mysql_fetch_array($query))
    {
    $i++;
    echo $code_part['count'];
   }
?>
于 2013-08-21T10:34:56.887 回答