PHP的未定义索引

This form is for a visitor sign in, which you can select the number of visitors with you from 1 - 9.

When selecting "1" and hitting submit I keep getting undefined index for "visitorname1".

PHP Form

<!doctype html>
 <html>
 <head>
 <meta charset="utf-8">
 <title>Visitor Sign In</title>
 <link rel="stylesheet" href="css.css" type="text/css" media="screen" />
 <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
 <meta name="viewport" content="width=device-width">
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
<script type="text/javascript">
    //when the webpage has loaded do this
    $(document).ready(function() {  
        //if the value within the dropdown box has changed then run this code            
        $('#noguests').change(function(){
            //get the number of fields required from the dropdown box
            var num = $('#noguests').val();                  

            var i = 0; //integer variable for 'for' loop
            var html = ''; //string variable for html code for fields 
            //loop through to add the number of fields specified
            for (i=1;i<=num;i++) {
                //concatinate number of fields to a variable
                html += 'Visitor ' + i + ': <input type="text" name="visitorname' + i + '" id="visitorname' + i + '"/><br/>'; 
            }

            //insert this html code into the div with id catList
            $('#guestList').html(html);
        });
    }); 
</script>
</head>
<body>
<p align="center"><img src="images/logo.jpg" width="300"></p>
<h1 align="center">Landmark Group of Builders :: Visitor Sign In</h1>
<table border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><span class="large">Please sign-in below:</span></td>
</tr>
<form id="form1" name="form1" method="post" action="submit_guest.php">
<tr>
<td bgcolor="#EAEAEA" align="right"><b>Visiting</b></td>
<td bgcolor="#EAEAEA"><input name="visiting" type="text" required id="visiting"></td>
</tr>
<tr>
<td bgcolor="#EAEAEA" align="right"><b>No. of Guests</b></td>
<td bgcolor="#EAEAEA">
<select id="noguests" name="noguests">
        <option value="0">- SELECT -</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
    </select>
</td></tr>
<td id="guestList">
</td></tr>
</tr>
<tr>
  <td colspan="2" align="right">
  <input type="text" name="stat" size="40" value="1" readonly class="hidden"><br><input type="submit" value="Sign In" class="css3button"></td>
</tr>
 </form>
 </table>
 </body>
 </html>

UPDATE.php

<?php
$intime = date('Y-m-d H:i:s');
$visitorname1 = $_POST['visitorname1'];
$visting = $_POST['visiting'];
$stat = $_POST['stat'];
$noguests = $_POST['noguests'];


$sql = new mysqli('localhost','x','x1!','x1');

$query = $sql->prepare("INSERT INTO `visitors` (visitorname1, visiting, intime, stat, noguests) VALUES (?,?,?,?,?);");

 $query->bind_param('sssii',$_POST['visitorname1'],$_POST['visiting'],$_POST['intime'],$_POST['stat'],$_POST['noguests']);

/* close our connection */
 $query ->execute();
 if ( $query ) {
echo "<p align='center' class='confirmation'><img src='images/logo.jpg' width='300px'><BR><BR><img src='images/accepted.png'>&nbsp;Thank You!  You have been signed in!</p>";
} else {
echo "Your request failed.  Please try again.";
}
$sql->close();
?>

I am not sure why I keep getting the undefined as I verified when I add a text box by selecting "1", the id and name of the textbox is "visitorname1".

Any help would be greatly appreciated.

Remove var i = 0; and:

change your:

for (i=1;i<=num;i++) {

to

for (var i = 0;i<=num;i++) {

Because your dynamic inputs have no value attribute.

In the javascript you have the NAME set to name="visitorname" while in the PHP you have $_POST['visitorname1'];

SOLUTION: change the PHP to $_POST['visitorname'];

It's because of using Form tag in a wrong place! correct your html code like this

    <!doctype html>
     <html>
     <head>
     <meta charset="utf-8">
     <title>Visitor Sign In</title>
     <link rel="stylesheet" href="css.css" type="text/css" media="screen" />
     <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
     <meta name="viewport" content="width=device-width">
     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
    <script type="text/javascript">
        //when the webpage has loaded do this
        $(document).ready(function() {  
            //if the value within the dropdown box has changed then run this code            
            $('#noguests').change(function(){
                //get the number of fields required from the dropdown box
                var num = $('#noguests').val();                  

                var i = 0; //integer variable for 'for' loop
                var html = ''; //string variable for html code for fields 
                //loop through to add the number of fields specified
                for (i=1;i<=num;i++) {
                    //concatinate number of fields to a variable
                    html += 'Visitor ' + i + ': <input type="text" name="visitorname' + i + '" id="visitorname' + i + '"/><br/>'; 
                }

                //insert this html code into the div with id catList
                $('#guestList').html(html);
            });
        }); 
    </script>
    </head>
    <body>
    <p align="center"><img src="images/logo.jpg" width="300"></p>
    <h1 align="center">Landmark Group of Builders :: Visitor Sign In</h1>
    <form id="form1" name="form1" method="post" action="submit_guest.php">
    <table border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
    <td colspan="2"><span class="large">Please sign-in below:</span></td>
    </tr>
    <tr>
    <td bgcolor="#EAEAEA" align="right"><b>Visiting</b></td>
    <td bgcolor="#EAEAEA"><input name="visiting" type="text" required id="visiting"></td>
    </tr>
    <tr>
    <td bgcolor="#EAEAEA" align="right"><b>No. of Guests</b></td>
    <td bgcolor="#EAEAEA">
    <select id="noguests" name="noguests">
            <option value="0">- SELECT -</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
        </select>
    </td></tr>
    <td id="guestList">
    </td></tr>
    </tr>
    <tr>
      <td colspan="2" align="right">
      <input type="text" name="stat" size="40" value="1" readonly class="hidden"><br><input type="submit" value="Sign In" class="css3button"></td>
    </tr>
     </table>
     </form>
     </body>
     </html>