javascript文件不会在php中触发

php:

<?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()';
    echo "</script>";
  }
>?

javascript:

 function emptyfield()
 {
if(document.forms ["add"] ["save"].value !== "")


 {

   document.getElementById('display').innerHTML="Please fill the blank fields!!!";

 }
 }

The java script file does not trigger in the php file. When I click the submit button, I need the div to be displayed as "Please fill the blank fields". But it does not happen in my case.

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

I think you forgot to echo the 'emptyfield()'; in php.

You're not echoing the emptyfield(); line.

Troubleshooting problems like this would be as simple as viewing source of the rendered markup in your browser, and you'd see that this line doesn't get written to the response.

Use this, You have did a error in PHP end tag ?> as well as change the script as how i mentioned.

<?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>';
  }
?>

I think you should do the checking after data was posted

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.

    }
}

or if you want to use client side checking of inputs create a function in javascript to check their value

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;
            }
   }

}

}

In the form you should do like this

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

</form>

Explanation:

When submitting the form, or hitting the submit button, checkIfBlank javascript function will be called passing the form myForm as its argument. checkIfBlank function then checks each element in myForm if it has a value. if not then highlights the element with red borders then focus on it then return false, which means the form will not be submitted. if all elements have value then it will return nothing thus proceeding in the submission of data.