Here is my code:
function myFunction2(){
var code = document.getElementById("vehicle").value;
var aux = "<?php
$conn = oci_connect($_SESSION['user'], $_SESSION['pswd'], 'oracleps');
$stid = oci_parse($conn,'select max(kmi) from
lloguer where lloguer.codi_vehicle="+code+"');
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_BOTH);
$kmi=($row[0]);
echo $kmi;
?>";
document.getElementById("kilometres").value= aux;}
I'm trying (very new on this) to update the id="vehicle" value, which is a text input, by calling the onclick="myFunction2()". The main problem i find it's that inside the php string, it doesn't allow me to concat the string with the "code" var in between.
I've tried to cocant the whole 'document.getElementById("vehicle").value'
Also tried by using the concat JS method.
What shall I do?
Thank you!
1 Answer 1
Yes you can achieve this,
- The file should be a
.phpfile - Keep it separate.
before entering into javascript you have to parse the value to a php variable first.
<?php
$conn = oci_connect($_SESSION['user'], $_SESSION['pswd'], 'oracleps');
$stid = oci_parse($conn,'select max(kmi) from lloguer where lloguer.codi_vehicle="+code+"');
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_BOTH);
$kmi=($row[0]);
?>
after this you are having the value in $kmi.
now the javascript part
<script type="text/javascript">
var aux = "<?php echo $kmi; ?>";
</script>
The above can be used if you want to access a php variable values in javascript, the below you can retrieve data using ajax.
Keep this in a separate file say ajax.val.php
<?php
$conn = oci_connect($_SESSION['user'], $_SESSION['pswd'], 'oracleps');
$stid = oci_parse($conn,'select max(kmi) from lloguer where lloguer.codi_vehicle="+code+"');
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_BOTH);
echo $row[0];
?>
in javascript
<script type="text/javascript">
$(document).ready(function(){
$("#vehicle").change(function(e){
$.ajax({
url : "ajax.val.php",
data:{
v : $(this).val()
},
success: function(e){
$("#kilometres").val(e);
}
});
});
});
</script>
2 Comments
"+code+" is invalid. (2) the OP wants to change the value of $kim/var aux when the getElementById("vehicle").value changes, which still is not possible with your code.Explore related questions
See similar questions with these tags.
codeis not defined/valid. When you javascript is executed, your php is already executed so it can't change the value ofaux. You need to use ajax if you want to use a javascript value to change a php code.