我有一个有下拉选项的联系人表单,现在我需要根据用户从下拉列表中单击的选项、联系人表单上"Action“的名称来更改,这样我就可以根据那里的选项分配一个不同的PHP文件。我知道我想要什么,我只是有一个困难的时间编码,使其发挥作用。我将提供下面的html表单和PHP。
<div id="form123">
<form id="my-form" action="sitephp.php" method="post">
<table width="343" border="0" align="center">
<tbody>
<tr>
<select name="Machine" required id="Machine" onChange="changeSelectValue();">
<option selected="selected"></option>
<option id="machine 1" value="sitephp1.php">machine 1</option>
<option value="sitephp2.php">machine 2</option>
<option id="machine 3" value="sitephp3.php">machine 3 </option>
</select>
<script type="text/javascript" >
function changeSelectValue() {
var myForm = document.querySelector('#my-form');
var selectValue = document.querySelector('#Machine').value;
var actionFile = '';
switch (selectValue) {
case 'machine 1':
actionFile = 'sitephp1.php';
break;
case 'machine 2':
actionFile = 'sitephp2.php';
break;
case 'machine 3':
actionFile = 'sitephp3.php';
break;
default:
break;
}
myForm.setAttribute('action', actionFile);
}
</script>
<?php
$email1=$_POST['Email1'];
$email2=$_POST['Email2'];
$from=$_POST['Email1'];
$email='YOUR EMAIL HERE';
$subject=" Request ";
$message=$_POST['Box'];
$machine=$_POST['Machine'];
$name=$_POST['Name'];
$phone=$_POST['Phone'];
$number=$_POST['Number'];
$message="Name: ".$name."\r\n"."Phone: ".$phone."\r\n"."Email: " .$from ."\r\n"."Machine: ".$machine."\r\n"."Problem: ".$message ;
if ($number !=10) {
die("You are not a human! or your answer was incorrect!, Please go back and try again.");
}
if(!filter_var($email1, FILTER_VALIDATE_EMAIL)) {
die("Invalid email ($email1)");
}
if ($email1 == $email2) {
mail ($email, $subject, $message, "from:".$from);
echo 'Thanks You for the Maintenance Request! We will Contact you shortly. ';
}
else {
echo "This ($email2) email address is different from ($email1).\n";
}
?>
发布于 2017-05-01 17:12:16
添加一个函数绑定到所选内容的onchange,如下所示:
<select name="Select" required id="Select" onchange="changeSelectValue();">在表单中添加一个id:
<form id="my-form" action="sitephp.php" method="post">使用以下JS代码根据所选值更改操作属性:
function changeSelectValue() {
var myForm = document.querySelector('#my-form');
var selectValue = document.querySelector('#Select').value;
if (selectValue.length > 0) {
myForm.setAttribute('action', selectValue);
}
}只需用文件更改file1.php、file2.php和file3.php即可。
如果您没有将JS文件附加到页面上,请将上面的JS代码包装在<script type="text/javascript"></script>中,并将其放在页面末尾的body end标记之前。
现在您更改了代码,将文件的名称直接调用到选项的值中。这样,您不再需要开关大小写,您可以直接使用select的值作为要使用的表单的action属性。
发布于 2017-05-01 17:19:33
最好有一个主*.php脚本,您可以在其中选择一个取决于用户输入的事件的进一步过程,例如:
switch ( $_POST['Machine'] ) {
case 0:
include 'script1.php';
break;
case 1:
include 'script2.php';
break;
case 2:
include 'script3.php';
break;
}https://stackoverflow.com/questions/43722947
复制相似问题