简体   繁体   English

将php变量传递给ajax url

[英]passing php variables to ajax url

*updated *更新

The problem is there a multiple variables and i can't pass the php echo. 问题是有多个变量,我不能通过PHP回声。 and i can't just run the script within the php due to the " conflict 我不能只是因为“冲突”而在php中运行脚本

<?php
.....other stuff

 $i=0;
 while ($i < $num) {

$id=mysql_result($result,$i, "StudentID");
$first=mysql_result($result,$i,"First");
$last=mysql_result($result,$i,"Last");
$date= date("Y.m.d");
echo "<div id=\"list\"><div id=\"id\">$id</div><div id=\"name\"> $first $last  
<button onclick=\"addHit($id,$first,$last,$classname,$day,$times,$date)\">Checkin</button></div></div>  ";

$i++;
}

echo"</div></div></div></div>";

?>
<script>
function addHit(id, first, last, classname, day, times, date)
{
$.ajax({
   type: "POST",
   url: "checkinuser.php",
   data: "id=id&first=first&last=last&classname=classname&day=day&times=times&date=date",
   success: function(msg){
     alert( "Data Saved: " + msg ); //Anything you want
   }
 });
}
</script>

As others have mentioned, you can't have PHP directly in the HTML, so you need to escape all those variables in a PHP code block. 正如其他人所提到的,你不能直接在HTML中使用PHP,因此你需要在PHP代码块中转义所有这些变量。 If this were my code though, I'd consider refactoring a bit, so that I could use JSON encoded parameters, eg: 如果这是我的代码,我会考虑重构一下,以便我可以使用JSON编码的参数,例如:

<?php
    $rawData = array('id'=>$id, 'first'=>$first, 'last'=>$last, ... );
    $hitData = json_encode($rawData);
?>

<button onclick="addHit(<?php echo $hitData ?>)">Checkin</button>

function addHit(inputData)
{
    $.ajax({
        type: "POST",
        url: "checkinuser.php",
        data: inputData
        success: function(msg) {
            alert( "Checked In! " + msg ); 
        }
    });
}

In response to your edit, if you want to keep the params the way you have them, I'd modify your while loop: 为了回应你的编辑,如果你想按照你的方式保留params,我会修改你的while循环:

<?php
$i=0;
while ($i < $num) {

    $id=mysql_result($result,$i, "StudentID");
    $first=mysql_result($result,$i,"First");
    $last=mysql_result($result,$i,"Last");
    $date= date("Y.m.d");
?>
<div id='list'>
    <div id='id'>
        <?php echo $id?>
    </div>
    <div id='name'>
        <?php echo "$first $last" ?>
        <button onclick='addHit( <?php echo "$id,$first,$last,$classname,$day,$times,$date" ?> )'
            Checkin
        </button>
    </div>
</div>

<?php 
    $i++;
}
?>

The goal here is to make it easier to read/write, so separating static code (eg HTML) from dynamic code as much as possible can be a good thing. 这里的目标是使其更易于读/写,因此尽可能将静态代码(例如HTML)与动态代码分离可能是一件好事。 One big advantage is that you don't have to worry about escaping quotes like you do, though a disadvantage is having a fair number of <?php> blocks. 一个很大的优点是你不必担心像你一样转义引号,但缺点是有相当数量的<?php>块。

Also note that you're setting id='id' for all these divs, which won't work since they'll all share one id when id's need to be unique on a given page. 另请注意,您为所有这些div设置了id ='id',这将无效,因为当id需要在给定页面上唯一时,它们将共享一个id。 You probably want to echo $id there too. 你可能也想在那里echo $id

Similarly, there's no reason to have echo"</div></div></div></div>"; 同样,没有理由让echo"</div></div></div></div>"; - move that outside the PHP block and just leave it as HTML. - 将其移到PHP块之外,然后将其保留为HTML。

Finally, in your javascript, you're data parameter is the literal string: 最后,在你的javascript中,你的数据参数是文字字符串:

"id=id&first=first&last=last&classname=classname&day=day&times=times&date=date"

This won't work as id will literally be 'id', first will be 'first', etc. 这不会起作用,因为id确实是'id',首先是'first'等等。

You need to either use the method I have above (where you pass the whole parameter), or build up the object to pass as your data param. 您需要使用上面的方法(传递整个参数的位置),或者构建要作为数据参数传递的对象。 .ajax() will take objects, so I'd make it an object, and pass that, eg: .ajax()将获取对象,所以我将它作为一个对象,然后传递它,例如:

function addHit(id, first, last, classname, day, times, date)
{
inputData = { id: id, first: first, etc, etc}
$.ajax({
   type: "POST",
   url: "checkinuser.php",
   data: inputData,
   success: function(msg){
     alert( "Data Saved: " + msg ); //Anything you want
   }
 });
}

Shouldn't the data look alike: 数据看起来不一样:

data: "id="+id+"&first="+first+"&last="+last+"&classname="+classname+"&day="+day+"&times="+times+"&date="+date,

You can also use the following format: 您还可以使用以下格式:

data : { id : id, first : first, last : last, classname : classname, day : day, times : times, date : date }

plus what mimiz said. 加上米米兹说的话。

You can't insert PHP variables into other code directly. 您不能直接将PHP变量插入到其他代码中。 Try echo ing them. 尝试echo它们。

<button onclick=\"addHit('<?php echo $id.","$first.",".$last.",",$classname.",".$day.",".$times.",".$date; ?>')\">Checkin</button>

try this : 尝试这个 :

<button onclick=\"addHit(<?php echo " '$id', '$first' ,'$last','$classname','$day', '$times','$date'"; ?>)\">Checkin</button>

Regards; 问候;

mimiz mimiz

Try changing this: 尝试改变这个:

type: "POST",

for this: 为了这:

type: "GET",

You should pass the variables and receive them in your PHP code by GET, since it is passing by url. 您应该传递变量并通过GET在PHP代码中接收它们,因为它是通过url传递的。

addHit('$id,$first,$last,$classname,$day,$times,$date') must be addHit($id,$first,$last,$classname,$day,$times,$date) addHit('$ id,$ first,$ last,$ classname,$ day,$ times,$ date')必须是addHit($ id,$ first,$ last,$ classname,$ day,$ times,$ date)

(tnx to Musa ) (tnx到Musa)

data: "id=id&first=first&last=last&classname=classname&day=day&times=times&date=date" must be data: "id=$id&first=$first&last=$last&classname=$classname&day=$day&times=$times&date=$date" 数据:“id = id&first = first&last = last&classname = classname&day = day&times = times&date = date”必须是数据:“id = $ id&first = $ first&last = $ last&classname = $ classname&day = $ day&times = $ times&date = $ date”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM