简体   繁体   English

检查PHP数组在JavaScript中是否为空

[英]Checking if a PHP array is empty in JavaScript

Hi I have a form where a user can enter one or more books into a DB. 嗨,我有一个表单,用户可以在其中向数据库输入一本或多本书籍。 Whenever a user enters one book and forgets to enter the title, a JavaScript alert comes and alerts him to enter a title. 每当用户输入一本书而忘记输入书名时,就会出现JavaScript警报并提醒他输入书名。 Now if he has two or more books and he forgets to enter the title, the alert doesn't show up. 现在,如果他有两本书或更多书,而他忘记输入书名,则不会显示警报。

This is my JavaScript function. 这是我的JavaScript函数。

function validateForm() 
{ 
 var a=document.forms["submit_books"]["title"].value; 
  if (a==null || a=="") 
    { 
    alert("Please enter a Title"); 
    return false; 
    }


 var status = false;     
 var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
      if (document.submit_books.email.value.search(emailRegEx) == -1) {
           alert("Please enter a valid email address.");
           return false;
      }
}

And Here is my PHP code 这是我的PHP代码

<form method="post" name="submit_books" onsubmit="return validateForm()" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                <?php for ($i=1; $i<$num_of_books + 1; $i++){
                    echo "<strong>Book # $i</strong><br><br>";
                    ?>


                    <label for="title">*Title</label>: <input type="text" id="title" size="60" name="title[]" autocomplete="off"/><br><br>

              <?php }?>
<input type="submit" name="submit" value="Submit Books">
</form>




I even tried putting the PHP array into a JavaScript one. 我什至尝试将PHP数组放入JavaScript数组中。

     <?
$js_array = json_encode($title);
echo "var title = ". $js_array . ";\n";
?>
var index = 1;
if( index < title.length)
{
    alert("Please enter a Title"); 
    return false; 
} 

There must be an easier way doing this 一定有更简单的方法可以做到这一点

You should be doing 你应该在做

var index = 1;
if( index > title.length )
{
    alert("Please enter a Title"); 
    return false; 
}

Since there is no record if title.length = 0, that is, if 1 > 0 then there is no title. 由于title.length = 0时没有记录,也就是说,如果1> 0则没有标题。

You can also check 您也可以检查

 if( title.length === 0 )

You can do this like: 您可以这样做:

 <?
echo "var title_length = ". count($title) . ";\n";
?>
var index = 1;
if( index > title_length)
{
    alert("Please enter a Title"); 
    return false; 
} 

Try to use inside html form 尝试在HTML表单中使用

<label> 
<span>Book Title: (required)</span> 
<input name="book" type="text" placeholder="Please enter your books title" required autofocus> 
</label>

Then use javascript to validate 然后使用javascript进行验证

(function() {

    // Create input element for testing
    var inputs = document.createElement('input');

    // Create the supports object
    var supports = {};

    supports.autofocus   = 'autofocus' in inputs;
    supports.required    = 'required' in inputs;
    supports.placeholder = 'placeholder' in inputs;

    // Fallback for autofocus attribute
    if(!supports.autofocus) {

    }

    // Fallback for required attribute
    if(!supports.required) {

    }

    // Fallback for placeholder attribute
    if(!supports.placeholder) {

    }

    // Change text inside send button on submit
    var send = document.getElementById('submit');
    if(send) {
        send.onclick = function () {
            this.innerHTML = '...Processing';
        }
    }

})();

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

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