PHP的未定义索引

时间:2014-02-03 21:49:44

标签: javascript php jquery

此表单适用于访客登录,您可以从1到9中选择与您一起的访客人数。

当选择“1”并点击提交时,我会继续为“visitorname1”获取未定义的索引。

PHP表格

<!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();
?>

当我通过选择“1”添加文本框时,我不确定为什么我继续获取未定义的文本框,文本框的ID和名称为“visitorname1”。

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:0)

删除var i = 0;和:

改变你的:

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

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

答案 1 :(得分:0)

因为您的动态输入没有value属性。

答案 2 :(得分:0)

在javascript中,您将NAME设置为name =“visitorname”,而在PHP中,您有$ _POST ['visitorname1'];

解决方案:将PHP更改为$ _POST ['visitorname'];

答案 3 :(得分:0)

这是因为在错误的地方使用Form标签! 像这样纠正你的HTML代码

    <!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>