javascript文件在php中不会触发

javascript文件在php中不会触发,javascript,php,Javascript,Php,php: java脚本文件不会在php文件中触发。当我点击提交按钮时,我需要div显示为“请填写空白字段”。但我的情况并非如此 echo“\n”; “emptyfield()”; 回声“; 我想你忘了回显“emptyfield()”。您没有回显emptyfield()行 解决此类问题的方法很简单,只需在浏览器中查看呈现标记的源代码,您就会看到这一行没有写入响应。使用此方法,您在PHP end tag?>中出现了错误,并且按照我提到的方式更改了脚本 echo "<script type='

php:

java脚本文件不会在php文件中触发。当我点击提交按钮时,我需要div显示为“请填写空白字段”。但我的情况并非如此

echo“\n”;
“emptyfield()”;
回声“;

我想你忘了回显
“emptyfield()”。

您没有回显
emptyfield()


解决此类问题的方法很简单,只需在浏览器中查看呈现标记的源代码,您就会看到这一行没有写入响应。

使用此方法,您在PHP end tag
?>
中出现了错误,并且按照我提到的方式更改了脚本

echo "<script type='text/javascript'>\n";
'emptyfield()';
echo "</script>";

我认为你应该在数据发布后进行检查

<?php
if (empty($itemId)|| (empty($title) && $title==="Please Select") 
   || empty($subtitle) || empty($descript)
    ||   empty($availability)
    || empty($price) || empty($filename))
  {
     echo '<script type="text/javascript">emptyfield()</script>';
  }
?>
或者如果您想使用客户端输入检查 在javascript中创建一个函数来检查它们的值

if($_SERVER['REQUEST']=='POST'){
   // checking condition
    $condition = (empty($itemId)|| (empty($title) && $title==="Please Select") 
      || empty($subtitle) 
      || empty($descript)
      ||   empty($availability)
      || empty($price) 
      || empty($filename)); 

    if($condition){
    // Throw an error here!
    echo "Please fill all the tables"; 

    }else{
    // execute query here.

    }
}
以你应该这样做的形式

function checkIfBlank(form){
   for(x in form){ 
   //checking only input boxes, select boxes and textarea
   if(form[x].nodeName=='INPUT' || form[x].nodeName=='SELECT' || form[x].nodeName=='TEXTAREA'){
      //checking of value if its empty.. if empty throw error
         if(form[x].value==''){
            if(document.getElementById(form[x].id)!=null){
            alert("Please enter a value in the fields with red borders.");

            document.getElementById(form[x].id).style.border="inset 3px red";
            document.getElementById(form[x].id).focus();

            return false;
            }
   }

}

}

//这里的元素。。
说明:

提交表单或点击提交按钮时,将调用
checkIfBlank
javascript函数,并将表单
myForm
作为其参数传递。
checkIfBlank
函数然后检查
myForm
中的每个元素是否有值
如果不是
则突出显示带有红色边框的元素,然后将焦点放在该元素上,然后返回false,这意味着表单将不会提交。如果
所有元素都有值
,那么它将不返回任何内容,因此将继续提交数据。

给出更多代码细节。这里我们甚至看不到你所说的公式。简单地说你有什么问题。因为浏览器已经在重新加载页面,所以不清楚选择这样的小脚本是否会在现有页面中弹出一些文本。从您发布的内容来看,不清楚这些代码片段应该如何组合在一起,但在我看来,即使考虑到缺少的
echo
,这也不会起作用
回显“emptyfield()”
if($_SERVER['REQUEST']=='POST'){
   // checking condition
    $condition = (empty($itemId)|| (empty($title) && $title==="Please Select") 
      || empty($subtitle) 
      || empty($descript)
      ||   empty($availability)
      || empty($price) 
      || empty($filename)); 

    if($condition){
    // Throw an error here!
    echo "Please fill all the tables"; 

    }else{
    // execute query here.

    }
}
function checkIfBlank(form){
   for(x in form){ 
   //checking only input boxes, select boxes and textarea
   if(form[x].nodeName=='INPUT' || form[x].nodeName=='SELECT' || form[x].nodeName=='TEXTAREA'){
      //checking of value if its empty.. if empty throw error
         if(form[x].value==''){
            if(document.getElementById(form[x].id)!=null){
            alert("Please enter a value in the fields with red borders.");

            document.getElementById(form[x].id).style.border="inset 3px red";
            document.getElementById(form[x].id).focus();

            return false;
            }
   }

}

}
<form name='myForm' type='post' action='yourphp.php' onsubmit='return checkIfBlank(this.form)' >
 //elements here..

</form>