Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从php脚本中检索选择选项?_Php_Javascript_Forms - Fatal编程技术网

无法从php脚本中检索选择选项?

无法从php脚本中检索选择选项?,php,javascript,forms,Php,Javascript,Forms,我有一个表单,它在提交时将数据发送到external.php脚本。我想禁用在数据库中插入两个同名学生的可能性。所以,若输入无效,我希望表单中输入的字段具有最后输入的值。我解决了这个问题,但我不知道如何检索最后选择的选项?这是我的代码: <form action="student.php" name="add-student" method="post" onSubmit="return Validate()"> Firstname:<input type="text" name

我有一个表单,它在提交时将数据发送到external.php脚本。我想禁用在数据库中插入两个同名学生的可能性。所以,若输入无效,我希望表单中输入的字段具有最后输入的值。我解决了这个问题,但我不知道如何检索最后选择的选项?这是我的代码:

<form action="student.php" name="add-student" method="post" onSubmit="return Validate()">
Firstname:<input type="text" name="firstname" value="<?php  if(isset($_GET['firstname'])){echo $_GET['firstname'];}

Degree:<select name="degree" value="<?php if(isset($_GET['degree'])){echo '<option value=$_GET[["degree"]></option>';} ?>"> **//This is what i can't solve!!!**
<option value="1">First degree</option>
<option value="2">Second degree</option>
<option value="3">Third degree</option>
</select>
</form>
我的外部php脚本:

<?php
$firtsname=$_POST['firstname'];
$degree=$_POST['degree'];

$duplicate=mysqli_query($con,"SELECT * FROM student WHERE firstname='$firstname'");
if(mysqli_num_rows($duplicate)>0)
{
$row = mysqli_fetch_assoc($duplicate);

echo '<script type="text/javascript">';
echo 'alert("Name already exists in db.")';
echo '</script>'; 
echo "<script>window.location.assign('http://localhost/administrator.php?firstname=".$firstname."&degree=".$degree."'); </script>";
}
else{
$sql="INSERT INTO student (firstname,degree) VALUES('$firstname','$degree')";
?>
换成这个

   Degree:<select name="degree" value="<?php if(isset($_GET['degree'])){echo '<option
    value='".$_GET["degree"]."' selected></option>';} ?>">

输入和下拉表单一起使用不正确。试试这个

<form action="student.php" name="add-student" method="post" onSubmit="return Validate()">
Firstname:<input type="text" name="firstname" value="<?php  if(isset($_GET['firstname'])){echo $_GET['firstname'];}?>">
</form>
Degree:<select name="degree"> 
<option value="<?php if(isset($_GET['degree'])){echo $_GET["degree"];}?>"></option>
<option value="1">First degree</option>
<option value="2">Second degree</option>
<option value="3">Third degree</option>
</select>

报价问题。这条线

<?php if(isset($_GET['degree'])){echo '<option value=$_GET[["degree"]></option>';} ?>
实际上会打印类似于-

<select name="degree" value="<option value=x</option>"> <!-- Wrong! -->
试试这个-

    <select name="degree">
    <?php if(isset($_GET['degree'])){$deg = $_GET["degree"]; echo "<option selected="selected" value=\"$deg\"></option>";} ?>">

当数据库能够工作时,让它来完成工作。在您的例子中,您使用PHP来处理许多实际的数据库操作

确保数据库中的列firstname的键设置为unique。 在查询中使用参数以避免sql注入 我会这样做:我没有测试过

<?php
$firstname=$_POST['firstname'];
$degree=$_POST['degree'];

$stmt = $mysqli->prepare("INSERT IGNORE INTO student (firstname,degree) VALUES(?, ?)");
$stmt->bind_param($firstname, $degree);
$result = $stmt->execute();
$lastInsertID = $stmt->insert_id;

//lastInsertID would be 0 if no execution of INSERT has been made (based on the IGNORE keyword)
if($lastInsertID == 0)  
{
    echo '<script type="text/javascript">';
    echo 'alert("Name already exists in db.")';
    echo '</script>'; 
    echo "<script>window.location.assign('http://localhost/administrator.php?firstname=".$firstname."&degree=".$degree."'); </script>";
}

?>
要解决不正确时检索选项的问题,请将行更改为:

Firstname:<input type="text" name="firstname" value="<?php if(isset($_GET['firstname'])){echo $_GET['firstname'];}?> " />
Degree:<select name="degree" value="<?php if(isset($_GET['degree'])){echo '<option value="' . $_GET["degree"] . '"></option>';} ?>">
同时将重定向更改为:

echo "<script type=\"text/javascript\">window.location.assign('http://localhost/administrator.php?firstname='.$firstname.'&degree='.$degree); </script>";

我解决了这个问题,谢谢大家的帮助,不管怎样,应该是这样的:

Degree:<select name="degree">
<option value="1" <?php if($_GET['degree']=='1') {?> selected="selected" <?php }; ?> >First degree</option>
<option value="2" <?php if($_GET['degree']=='2') {?> selected="selected" <?php }; ?> >Second degree degree</option>
<select>

是否要上次插入值?如果输入vas无效,我希望上次输入保持在字段中,上次选择的选项保持原样。我正在做一个有10个输入字段和2个选择的大型表单的项目,所以如果我的输入无效,我想保留最后输入的值,所以我不需要再次键入它…你能给我们演示一下验证函数吗?不需要显示,它很简单,如果firstname为空,它不允许将数据从表单发送到脚本…@IvanPandžić-这不是关于firstname是否为空,而是关于不插入重复项。我不知道为什么,但当我被重定向回表单时,它仍然不起作用,选项度数设置为默认值=1…:/i已在此行中插入select=selected,但再次,nothing@IvanPandžić你看过我回答中的更新了吗?您的标记格式不正确。解决这个问题。而且它的not select=selected,它的is selected=selected将不再工作。现在,当它将我重定向到我的表单时,有第四个选项被选中,但它是空的…value=$\u GET[[degree]将值设置为字符串$\u GET[[degree]而不是实际值。还有一个括号。我快疯了,所有这些都不起作用,谢谢大家的帮助,但看起来我需要一些说明…看我上次编辑时,我错过了一个重要的引号。然后发生了什么?错误?hm$firtsname应该是$firstname。我已经更改了它。它对POS不起作用无论如何…因为我是php新手,所以请原谅,如果我错了,我会将数据名和学位发送回地址栏中的te表单页面,其中有一行:echo window.location.assign';所以我用$_GET…啊哈…好的,那么应该是$_GET。请原谅,我错过了这一行…我会编辑。
echo "<script type=\"text/javascript\">window.location.assign('http://localhost/administrator.php?firstname='.$firstname.'&degree='.$degree); </script>";
Degree:<select name="degree">
<option value="1" <?php if($_GET['degree']=='1') {?> selected="selected" <?php }; ?> >First degree</option>
<option value="2" <?php if($_GET['degree']=='2') {?> selected="selected" <?php }; ?> >Second degree degree</option>
<select>