PHP内部javascript复选框值


php inside javascript checkbox value

我有一个列表列表的复选框与来自DB的文件名称。然后我有删除文件的按钮。我有下面的按钮代码:

<input type='button' id='submit_btn' onclick='eraseFile()'  value='DELETE FILES' />

和eraseFile函数…

<script type="text/javascript" language="javascript">
function eraseFile(){
    var checekedFiles = [];
    $('input:checked').each(function() {
        checekedFiles.push($(this).val());
    });
    alert(checekedFiles); // it gives me all the checked values..good
    <?php
        echo "HElllo World";
    ?>
}
</script>

给出一个错误"missing;before语句"answers"eraseFile is not defined"

是否有可能在javascript内写php ??

是否有可能在javascript内写php ??

除非PHP代码正在生成有效的JavaScript,否则不行。

eraseFile被称为undefined的原因是您的echo语句导致语法错误,因为它在JavaScript函数的末尾打印字符串文字Hellllo World,这违反了JavaScript语法规则。

是可能的。

PHP是在服务器上解析的,因此您将在javascript函数中打印" hello World",这可能会导致错误。

你可能正在寻找做以下事情:

<?php echo 'document.write("Hello World!");'; ?>

PHP输出被附加到JS函数中,使javascript看起来像这样:

<script type="text/javascript" language="javascript">
function eraseFile(){
    var checekedFiles = [];
    $('input:checked').each(function() {
        checekedFiles.push($(this).val());
    });
    alert(checekedFiles); // it gives me all the checked values..good
    HElllo World //syntax error here
}
</script>

你可以这样做:

<script type="text/javascript" language="javascript">
function eraseFile(){
    var checekedFiles = [];
    $('input:checked').each(function() {
        checekedFiles.push($(this).val());
    });
    alert(checekedFiles); // it gives me all the checked values..good
    alert("<?php echo "HElllo World"; ?>");
}
</script>

这会弹出一个提示'Hello World'

要将Javascript函数的值传递给PHP脚本,可以这样做:
var yourJsVar = {assign value here};
url = "yourPHPScript.php?value=" + yourJsVar;
if (window.XMLHttpRequest) 
{ // Non-IE browsers 
  req = new XMLHttpRequest(); 
  req.onreadystatechange = someFunction;  
  //someFunction will get called when the PHP script is done executing
  try 
  { 
    req.open("GET", url, true); 
  } 
  catch (e) 
  { 
    alert(e); 
  } 
  req.send(null); 
} 
else if (window.ActiveXObject) 
{ // IE 
  req = new ActiveXObject("Microsoft.XMLHTTP"); 
  if (req) 
  { 
    req.onreadystatechange = someFunction; 
    req.open("GET", url, true); 
    req.send();                                 
  } 
} 

在PHP脚本中:

$yourPhpVar = $_GET['value'];

我在上面提到someFunction,它在PHP脚本完成执行后被调用。它应该是这样的。(注意,这是在你的Javascript)

function someFunction()
{
    if(req.readyState == 4 && req.status == 200)
    {
        //this will only execute after your AJAX call has completed.
        //any output sent by your PHP script can be accessed here like this:
        alert(req.responseText);
    }
}

尝试回显一个有意义的javascript代码,"Hello World"这不是一个有效的JS语句。

试试

<?php
    echo "alert('HElllo World');";
?>

您的eraseFile函数定义在哪里?

如果在调用位置之后才定义,则会得到该错误。


附注:
你可以在javascript里面有php echo,除了你有什么不会做太多…

是的,您可以在脚本文件中使用PHP代码,但是您的代码在这里生成无效的脚本代码。

<?php
    echo "HElllo World";  // becomes: HElllo World (text!) in JS
?>

可以用Javascript编写PHP,但这不是最佳实践。我们通常是通过AJAX实现的。阅读文档:http://api.jquery.com/category/ajax/

是的,在JavaScript中包含PHP是可能的,因为PHP将在页面内容发送到客户端之前在服务器上执行。但是,在您的情况下,发送的内容如下:

<script type="text/javascript" language="javascript">
function eraseFile(){
    var checekedFiles = [];
    $('input:checked').each(function() {
        checekedFiles.push($(this).val());
    });
    alert(checekedFiles); // it gives me all the checked values..good
    HElllo World
}
</script>

不能验证为JavaScript,因为" hello World"不是一个有效的JavaScript命令。这就是函数没有被正确定义的原因。您需要将"hello World"字符串替换为实际的JavaScript命令。