将Javascript变量解析为php查询

时间:2013-08-21 09:51:33

标签: php javascript parsing

我是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()"没有提交的情况下调用该函数。

任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

最佳方法是Ajax

否则使用Cookies

例如:

<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);
}

答案 1 :(得分: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>
    
  3. newfile.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'];
       }
    ?>