0

I am newbie in php and still learn javascript function.

I have a problem with, include javascript variable into php query.

I want to create transaction code according count of part code which have different type. then i must get value with count.

<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>

The result like that 1/CPU/JKT/2013

I call that function with button onClick="getCode()" no submit.

Anyone can help me.

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
okywijaya
  • 59
  • 2
  • 13
  • 1
    You can't combine `php` and `js` code in one block and expect variables to be available in both. You can learn it [here](http://www.dyn-web.com/tutorials/php-js/) – Salman Aug 21 '13 at 09:55
  • Hint --> use the "tools -> view source" option in your browser. php can only be used for generating the html/jscript page. Once generated php is done and gone. – James Anderson Aug 21 '13 at 09:59
  • you're opening yourself to any kind of possible vulnerability and security holes with that code. – Saturnix Aug 21 '13 at 10:26
  • possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – Quentin Aug 21 '13 at 10:45
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Aug 21 '13 at 10:45
  • Thanks All for your attention.. I use ajax and post with json. I found the answer in.. [link] here (http://www.technicalkeeda.com/details/simple-ajax-post-example-with-php-codeIgniter-framework-and-jquery) – okywijaya Aug 22 '13 at 02:54

2 Answers2

0

Best Method is Ajax

Otherwise Use Cookies

For Eg:

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

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

Your code

<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>

Updated

Check this

function getCode(){
var v = 12;
document.cookie = "cookieName="+v;
var getPartCode = <?php echo $_COOKIE['cookieName']; ?>
alert(getPartCode);
}
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
  • I have follow your (Nathan Srivi) instruction to use cookie, but still not working. I try call that cookie with javascript alert still not alert show. The Script Like This.. – okywijaya Aug 21 '13 at 10:30
0

You can't do it by the way you tried.There are two better ways to do it

  1. Use Ajax.

  2. Use Cookies

    The best way here is to use Ajax. Here, iam using javascript and ajax.And i am creating a new file to create response(say newfile.php) Try this,

    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>
    

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'];
   }
?>
raduns
  • 765
  • 7
  • 18
  • could do indent your code? Even if i know what you are trying to do it's kind of difficult to read it – fGo Aug 21 '13 at 11:11
  • raduns, I use code igniter framework. i dont know how to load view .. in thats script xmlhttp.open("GET","newFile.php?q="+v,true); Thanks for the reply – okywijaya Aug 22 '13 at 01:39