[英]Passing variable from javascript to PHP using ajax in yii
I want to pass variable from javascript to php using POST and I am doing something like the following. 我想使用POST将变量从javascript传递到php,而我正在做类似以下的事情。 I view1.php
我view1.php
<script>
function testing(col) {
$("#bookId").val(col);
$.ajax({
type: 'POST',
url: <?php echo Yii::app()->createUrl('siteaccess/create') ?>,
data: {ad_id:<?php echo "hello" ?>},
success: function(col){console.log(col)},
});
}
</script>
In same file I have following code calling testing() 在同一文件中,我有以下代码调用testing()
function(){
testing($(this).parent().parent().children(\':nth-child(2)\').text());
}
In create.php I have 在create.php中,我有
<?php
$v = $_POST['ad_id'];
echo $v;
?>
For create.php I am getting this error "Undefined index: ad_id". 对于create.php,我收到此错误“未定义索引:ad_id”。 Can anyone guide where I am making mistake?
谁能指导我在哪里出错?
You have missed the semicolumn in the php code and also define the dataType in the ajax request: 您已经错过了php代码中的分号,并且还在ajax请求中定义了dataType:
$.ajax({
type: 'POST',
url: "<?php echo Yii::app()->createUrl('siteaccess/create'); ?>",
data: {"ad_id":"<?php echo 'hello'; ?>"},
dataType: 'text',
success: function(col){console.log(col)},
});
use the above code. 使用上面的代码。
Neither your URL or ad_id
contain quotes so it won't work. 您的URL或
ad_id
均不包含引号,因此无法使用。 Try this: 尝试这个:
<script>
function testing(col) {
$("#bookId").val(col);
$.ajax({
type: 'POST',
url: "<?php echo Yii::app()->createUrl('siteaccess/create') ?>",
data: {ad_id:"<?php echo "hello" ?>"},
success: function(col){console.log(col)},
});
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.