admin管理员组文章数量:1026087
I have a php code that provides the database values. I need those values in the javascript variable.
Javascript Code
<script src=".8.0.js"></script>
<script type="text/javascript">
function text() {
var textVal=$("#busqueda_de_producto").val();
$.ajax(
{
type:"POST",
url:"index.php", //here goes your php script file where you want to pass value
data: textVal,
success:function(response)
{
// JAVSCRIPT VARIABLE = varable from PHP file.
}
});
return false;
}
</script>
PHP FILE CODE:
<?php
$q11 = "select * from sp_documentopra_detalle where dcd_codigo".$_GET['codigo'];
$res11 = mysql_query($q11);
$row11 = mysql_fetch_array($res11);
?>
I have a php code that provides the database values. I need those values in the javascript variable.
Javascript Code
<script src="http://code.jquery./jquery-1.8.0.js"></script>
<script type="text/javascript">
function text() {
var textVal=$("#busqueda_de_producto").val();
$.ajax(
{
type:"POST",
url:"index.php", //here goes your php script file where you want to pass value
data: textVal,
success:function(response)
{
// JAVSCRIPT VARIABLE = varable from PHP file.
}
});
return false;
}
</script>
PHP FILE CODE:
<?php
$q11 = "select * from sp_documentopra_detalle where dcd_codigo".$_GET['codigo'];
$res11 = mysql_query($q11);
$row11 = mysql_fetch_array($res11);
?>
Share
Improve this question
edited Sep 1, 2012 at 17:03
mtk
13.8k16 gold badges75 silver badges116 bronze badges
asked Aug 30, 2012 at 9:49
Harsimran SinghHarsimran Singh
7484 gold badges13 silver badges29 bronze badges
2
-
1
In your PHP code, just do
echo json_encode($row11);
– Ja͢ck Commented Aug 30, 2012 at 9:56 - if you just want to pass variables from one php page to another just use SESSION. you are just getting variable from a script and passing back to index.php what is the use of them in you html/javascript page ? – GajendraSinghParihar Commented Aug 30, 2012 at 9:56
6 Answers
Reset to default 2Your returning data is in the response
parameter. You have to echo
your data in PHP to get the results
Using JSON format is convenient
because of its key-value nature.
Use json_encode to convert PHP array to JSON.
echo
the json_encoded variable
you will be able to receive that JSON response data through $.ajax
JavaScipt/HTML:
<script src="http://code.jquery./jquery-1.8.0.js"></script>
<script type="text/javascript">
function text()
{
var textVal=$("#busqueda_de_producto").val();
$.post('index.php', { codigo:textVal }, function(response) {
$('#output').html(response.FIELDNAME);
}, 'json');
return false;
}
</script>
<span id="output"></span>
PHP:
$q11 = "select * from sp_documentopra_detalle where dcd_codigo='".mysql_escape_string($_POST['codigo'])."'";
$res11 = mysql_query($q11);
$row11 = mysql_fetch_array($res11);
echo json_encode($row11);
You aren't echoing anything in your PHP script.
Try altering your PHP to this:
<?php
$q11 = "select * from sp_documentopra_detalle where dcd_codigo".$_GET['codigo'];
$res11 = mysql_query($q11);
$row11 = mysql_fetch_array($res11);
echo $row11; //This sends the array to the Ajax call. Make sure to only send what you want.
?>
Then in your Ajax call you can alert this by writing alert(response)
in your success handler.
Tips
Send your data to the server as a URL serialised string : request=foo&bar=4
. You can also try JSON if you fancy it.
Don't use mysql_*
PHP functions as they are being deprecated. Try a search for PHP Data Objects (PDO).
i see lots of things that needs to be corrected
the data in your ajax do it this way data: {'codigo':textVal},
since you are using $_GET['codigo'], which leads to the second correction, you used type:"POST"
so you must also access the $_POST variable and not the $_GET variable and lastly the target of your ajax does not display / return anything you either echo
it or echo json_encode()
it
The best solution is to use echo json_encode("YOUR ARRAY/VALUE TO BE USED");
and then parse JSON in the javascript code as obj = JSON.parse(response);
I have a php code that provides the database values. I need those values in the javascript variable.
Javascript Code
<script src=".8.0.js"></script>
<script type="text/javascript">
function text() {
var textVal=$("#busqueda_de_producto").val();
$.ajax(
{
type:"POST",
url:"index.php", //here goes your php script file where you want to pass value
data: textVal,
success:function(response)
{
// JAVSCRIPT VARIABLE = varable from PHP file.
}
});
return false;
}
</script>
PHP FILE CODE:
<?php
$q11 = "select * from sp_documentopra_detalle where dcd_codigo".$_GET['codigo'];
$res11 = mysql_query($q11);
$row11 = mysql_fetch_array($res11);
?>
I have a php code that provides the database values. I need those values in the javascript variable.
Javascript Code
<script src="http://code.jquery./jquery-1.8.0.js"></script>
<script type="text/javascript">
function text() {
var textVal=$("#busqueda_de_producto").val();
$.ajax(
{
type:"POST",
url:"index.php", //here goes your php script file where you want to pass value
data: textVal,
success:function(response)
{
// JAVSCRIPT VARIABLE = varable from PHP file.
}
});
return false;
}
</script>
PHP FILE CODE:
<?php
$q11 = "select * from sp_documentopra_detalle where dcd_codigo".$_GET['codigo'];
$res11 = mysql_query($q11);
$row11 = mysql_fetch_array($res11);
?>
Share
Improve this question
edited Sep 1, 2012 at 17:03
mtk
13.8k16 gold badges75 silver badges116 bronze badges
asked Aug 30, 2012 at 9:49
Harsimran SinghHarsimran Singh
7484 gold badges13 silver badges29 bronze badges
2
-
1
In your PHP code, just do
echo json_encode($row11);
– Ja͢ck Commented Aug 30, 2012 at 9:56 - if you just want to pass variables from one php page to another just use SESSION. you are just getting variable from a script and passing back to index.php what is the use of them in you html/javascript page ? – GajendraSinghParihar Commented Aug 30, 2012 at 9:56
6 Answers
Reset to default 2Your returning data is in the response
parameter. You have to echo
your data in PHP to get the results
Using JSON format is convenient
because of its key-value nature.
Use json_encode to convert PHP array to JSON.
echo
the json_encoded variable
you will be able to receive that JSON response data through $.ajax
JavaScipt/HTML:
<script src="http://code.jquery./jquery-1.8.0.js"></script>
<script type="text/javascript">
function text()
{
var textVal=$("#busqueda_de_producto").val();
$.post('index.php', { codigo:textVal }, function(response) {
$('#output').html(response.FIELDNAME);
}, 'json');
return false;
}
</script>
<span id="output"></span>
PHP:
$q11 = "select * from sp_documentopra_detalle where dcd_codigo='".mysql_escape_string($_POST['codigo'])."'";
$res11 = mysql_query($q11);
$row11 = mysql_fetch_array($res11);
echo json_encode($row11);
You aren't echoing anything in your PHP script.
Try altering your PHP to this:
<?php
$q11 = "select * from sp_documentopra_detalle where dcd_codigo".$_GET['codigo'];
$res11 = mysql_query($q11);
$row11 = mysql_fetch_array($res11);
echo $row11; //This sends the array to the Ajax call. Make sure to only send what you want.
?>
Then in your Ajax call you can alert this by writing alert(response)
in your success handler.
Tips
Send your data to the server as a URL serialised string : request=foo&bar=4
. You can also try JSON if you fancy it.
Don't use mysql_*
PHP functions as they are being deprecated. Try a search for PHP Data Objects (PDO).
i see lots of things that needs to be corrected
the data in your ajax do it this way data: {'codigo':textVal},
since you are using $_GET['codigo'], which leads to the second correction, you used type:"POST"
so you must also access the $_POST variable and not the $_GET variable and lastly the target of your ajax does not display / return anything you either echo
it or echo json_encode()
it
The best solution is to use echo json_encode("YOUR ARRAY/VALUE TO BE USED");
and then parse JSON in the javascript code as obj = JSON.parse(response);
本文标签: Pass PHP value to javascript using AjaxStack Overflow
版权声明:本文标题:Pass PHP value to javascript using Ajax - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745629022a2160044.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论