Page 1 of 2
Need Help - Javascript passing value
Posted: Tue Oct 11, 2005 8:39 am
by pido
Hi i have problem solving this javascript function to get return value of my PHP SQL query... Ok this are what i got
Code: Select all
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tblDetail">
<tbody>
<tr>
<td class="header" width="25">No.</td>
<td class="header" width="167">Part Number</td>
<td class="header" width="41">Qty</td>
<td class="header" width="101">Harga Offer</td>
<td class="header" width="*">Description</td>
<td class="header" width="40">Stok</td>
<td class="header" width="100">Harga (Rp)</td>
<td class="header" width="100">Harga (US$)</td>
</tr>
<tr>
<script language="JavaScript">
function queryPartNumber(){
var partNo = partNumber.value;
<?php
if (isset($partNumber) && !empty($partNumber)){
echo $partNumber;
$id = $partNumber;
$query = "SELECT KD_PRO,NM_PRO,STOK_AKHIR,HRG_JUAL_RP,UT_UPUS
FROM IM_PRODUCT WHERE FORMULA_ID = '$id'";
$result = mssql_query($query) or die('Invalid Part Number');
$row = mssql_fetch_array($result);
mssql_free_result($result);
}
?>
document.getElementById('partName').innerHTML = '<font color="#BB8B00"><? echo "$row[1]"; ?></font>';
}
</script>
<td class="content" align="center">1</td>
<td class="content" align="center"><input type="text" class="detail" name="partNumber" id="txtRow1" size="24" onChange="queryPartNumber();"></td>
<td class="content" align="center"><input type="text" class="detail" name="quantity" id="txtRow1" size="3"></td>
<td class="content" align="center"><input type="text" class="detail" name="harga" id="txtRow1" size="13"></td>
<td class="content" id="partName"></td>
<td class="integer" id="qty" align="center"><font color="#BB8B00">12</font></td>
<td class="integer" id="priceRP" align="right"><font color="#BB8B00">1,000,000.00</font></td>
<td class="integer" id="priceUS" align="right"><font color="#BB8B00">$ 1,000.00</font></td>
</tr>
</tbody>
</table>
In this prob, i do want user type a value in input text name="partNumber" and PHP do a query with that partNumber as my WHERE condition, thus display the result value in my other columns.
Question are:
1. Can i pass JS value to PHP value??? like in the code
var partNo = partNumber.value; to
$id = $partNo; because i want to do a query where the WHERE condition is that value i get from JS
2. Is it matter if i put the PHP code rather inside or outside <script language="Javascript>???
3. Does anyone have a beeter idea how to make that query run???
Posted: Tue Oct 11, 2005 8:46 am
by feyd
- passing data from Javascript to PHP is most often done via a form submission.
- not needed in the least if you aren't actually outputing Javascript with it.
Posted: Tue Oct 11, 2005 9:45 am
by pido
1. passing data from Javascript to PHP is most often done via a form submission.
2. not needed in the least if you aren't actually outputing Javascript with it.
Thnx for the answers feyd, I did put form in my page but it was refreshing it self I don't want that happened, i just want when the user input the value in field then the information related display in other fields.
Does anyone have another solution??? I'm dying here... T_T
Posted: Tue Oct 11, 2005 10:17 am
by feyd
unless you want to use XMLHTTP/AJAX, you must do form submission.
Posted: Wed Oct 12, 2005 6:41 am
by foobar
feyd wrote:unless you want to use XMLHTTP/AJAX, you must do form submission.
Some handy links to get you started:
http://en.wikipedia.org/wiki/AJAX
http://developer.mozilla.org/en/docs/AJAX
Posted: Wed Oct 12, 2005 11:44 am
by pido
Ok lets postpone my earlier problem, somehow I figured it out to use javascript to write a PHP code.
And now i had a new problem escaping JS to PHP
Code: Select all
var TempPartNo = partNumber.value;
document.write = '<? $query = "SELECT KD_PRO,NM_PRO,STOK_AKHIR,HRG_JUAL_RP,UT_UPUS FROM IM_PRODUCT WHERE FORMULA_ID = '?>+TempPartNo+<?';?>';
How can i put var TempPartNo in my WHERE condition??? cuz when i display my $query it goes
SELECT KD_PRO,NM_PRO,STOK_AKHIR,HRG_JUAL_RP,UT_UPUS FROM IM_PRODUCT WHERE FORMULA_ID = '?>+TempPartNo+<?'
Posted: Wed Oct 12, 2005 11:54 am
by foobar
pido wrote:Ok lets postpone my earlier problem, somehow I figured it out to use javascript to write a PHP code.
And now i had a new problem escaping JS to PHP
Code: Select all
var TempPartNo = partNumber.value;
document.write = '<? $query = "SELECT KD_PRO,NM_PRO,STOK_AKHIR,HRG_JUAL_RP,UT_UPUS FROM IM_PRODUCT WHERE FORMULA_ID = '?>+TempPartNo+<?';?>';
How can i put var TempPartNo in my WHERE condition??? cuz when i display my $query it goes
SELECT KD_PRO,NM_PRO,STOK_AKHIR,HRG_JUAL_RP,UT_UPUS FROM IM_PRODUCT WHERE FORMULA_ID = '?>+TempPartNo+<?'
What do you want exactly?

Posted: Wed Oct 12, 2005 12:12 pm
by pido
foobar wrote:What do you want exactly?

"What do i really want?" OK let make things clear, first of all i want JS do a query in my PHP with a WHERE condition, i have var called TempPartNo with a value from text that user typed in partNumber.value let say user put
123456, thus when user press tab (OnChange) JS write a PHP Query which is
SELECT KD_PRO,NM_PRO,STOK_AKHIR,HRG_JUAL_RP,UT_UPUS FROM IM_PRODUCT WHERE FORMULA_ID = '123456'
as I mentioned before
Code: Select all
var TempPartNo = partNumber.value;
document.write = '<? $query = "SELECT KD_PRO,NM_PRO,STOK_AKHIR,HRG_JUAL_RP,UT_UPUS FROM IM_PRODUCT WHERE FORMULA_ID = '?>+TempPartNo+<?';?>';
The query can't pass TempPartNo's value in my query and when I displayed the $query in page it give me
Code: Select all
SELECT KD_PRO,NM_PRO,STOK_AKHIR,HRG_JUAL_RP,UT_UPUS FROM IM_PRODUCT WHERE FORMULA_ID = '?>+TempPartNo+<?'
How can i put my TempPartNo's value in that query????????? T_T
Posted: Wed Oct 12, 2005 12:13 pm
by Chris Corbyn
It's the same problem... you're trying to pass from JS to PHP.
The problem lies in the fact JS is running on the client machine whereas PHP has already run on the server. It's easy enough to output a JavaScript value using PHP but the opposite requires communication with the server. Hence XMLHttp or AJAX. There's no easy anser unfortunately. You will need to use AJAX to do it cleanly which will invlove some learning.
Perhaps this might help...
xAJAX
I can think of an awful old-fashioned way....
Once that JavaScript variable is set you could do a
Code: Select all
window.location = '?partNo='+TempPartNo; //Redirect with a GET var
Then PHP can read $_GET['partNo'] but it's a bit messy

Posted: Wed Oct 12, 2005 3:44 pm
by pido
Umm... I think i'm not trying to passing a JS var to PHP var, just wana escaping the PHP in document.write = "<?php bla bla..." so that JS can read var TempPartNo.
Posted: Wed Oct 12, 2005 5:37 pm
by Chris Corbyn
pido wrote:Umm... I think i'm not trying to passing a JS var to PHP var, just wana escaping the PHP in document.write = "<?php bla bla..." so that JS can read var TempPartNo.
It doesn't make much sense so all I can do is look at your code and what you're saying here

It still looks like you're trying to use javascript inside PHP which isn't going to work.
Are you trying to use that JS var as part of the mysql query? If you are then my last two suggestions still stand. If not then I'm stumped as to what you're actually trying to do

Posted: Fri Oct 14, 2005 8:11 am
by pido
Well ok it isn't goin to work T_T your last two suggestions is make a form and pass the value to PHP right?? well i tried, but it gives me a new page, hmm... can i submit a form without refreshing the page?? my experienced I don't think so, now I'm really stucked here guys T_T
Any solution??? cuz i don't even know how to use what you called AJAX...
Posted: Fri Oct 14, 2005 8:54 am
by foobar
pido wrote:hmm... can i submit a form without refreshing the page??
1. No.
2. You can use a hidden iframe as a target like so:
Code: Select all
<form ... target="theiframe"> ... </form>
<iframe style="display: none;" name="theiframe"></iframe>
pido wrote:i don't even know how to use what you called AJAX...
1.
Google is your friend.
2.
Mozilla.org AJAX: Getting Started you baby.
Posted: Fri Oct 14, 2005 9:27 am
by pido
Ok i try this
Code: Select all
?><table width="100%" border="0" cellspacing="0" cellpadding="0" id="tblDetail">
<tbody>
<tr>
<td class="header" width="25">No.</td>
<td class="header" width="167">Part Number</td>
<td class="header" width="41">Qty</td>
<td class="header" width="101">Harga Offer</td>
<td class="header" width="*">Description</td>
<td class="header" width="40">Stok</td>
<td class="header" width="100">Harga (Rp)</td>
<td class="header" width="100">Harga (US$)</td>
</tr>
<tr>
<form action="index.php" target="hiddenIFrame.php" method="GET">
<td class="content" align="center">1</td>
<td class="content" align="center"><input type="text" class="detail" name="partNumber" id="txtRow1" size="24" onChange="this.form.submit();"></td>
<td class="content" align="center"><input type="text" class="detail" name="quantity" id="txtRow1" size="3"></td>
<td class="content" align="center"><input type="text" class="detail" name="harga" id="txtRow1" size="13"></td>
<?
if (isset($_GET['partNumber'])){
$id = $HTTP_GET_VARS["partNumber"];
$query = "SELECT NM_PRO,STOK_AKHIR,HRG_JUAL_RP,UT_UPUS FROM IM_PRODUCT WHERE FORMULA_ID = '".$id."'";
$result = mssql_query($query) or die();
$row = mssql_fetch_array($result);
mssql_free_result($result);
?>
<td class="content" id="partName"><? echo "$id"; ?></td>
<td class="integer" id="qty" align="center"><font color="#BB8B00">1</font></td>
<td class="integer" id="priceRP" align="right"><font color="#BB8B00">1,000,000.00</font></td>
<td class="integer" id="priceUS" align="right"><font color="#BB8B00">$ 1,000.00</font></td>
<?
}
else{
?>
<td class="content" id="partName"> </td>
<td class="integer" id="qty" align="center"><font color="#BB8B00">1</font></td>
<td class="integer" id="priceRP" align="right"><font color="#BB8B00">1,000,000.00</font></td>
<td class="integer" id="priceUS" align="right"><font color="#BB8B00">$ 1,000.00</font></td>
<?
}
?>
</tr>
</form>
<iframe style="display: none;" name="hiddenIFrame.php"></iframe>
</tbody>
</table>
Still can get partNumber value???
Posted: Fri Oct 14, 2005 9:41 am
by foobar
I don't think you understand what you are doing. Read the
PHP Manual, as you don't seem to grasp the concept behind what you are doing. Do you understand how PHP and JS work? I doubt it.
Here's a quick ref:
-PHP is interpreted on the _server_, ie. your webhost. What your browser gets is HTML. No PHP ever gets to the browser.
-JS is interpreted on the _client_ machine, ie. in your browser. Your PHP does not see what your JS is doing. Ever. (Unless you use AJAX, but I thinik that would be too difficult for you at this stage.)
Please explain _exactly_ what you want to achieve, not in terms of your JS variable, but the desired effect to the user. With every post you're confusing me more and more.
Edit: I looked through your code and I don't have a clue what the hell you're trying to do. Your code prints out an item's info based on its ID. What else do you want it to do?