Reputation: 125
I have a Contact form that has a drop down with options, now I need depending on the option the person clicks on from the drop down, The name of the "Action" on Contact form to change, that way I can assign a different PHP FILE depending on there option. I know what I want im just having a hard time coding this to make it work. I'll provide the html form and the PHP below.
<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";
}
?>
Upvotes: 1
Views: 186
Reputation: 12689
It would be better to have one main *.php
script where you choose a further course of events that depends on the user's input, for example:
switch ( $_POST['Machine'] ) {
case 0:
include 'script1.php';
break;
case 1:
include 'script2.php';
break;
case 2:
include 'script3.php';
break;
}
Upvotes: 1
Reputation: 9988
Add a function bind to the onchange of your select as below:
<select name="Select" required id="Select" onchange="changeSelectValue();">
Add an id to your form:
<form id="my-form" action="sitephp.php" method="post">
Use the following JS code to change your action property depending on the selected value:
function changeSelectValue() {
var myForm = document.querySelector('#my-form');
var selectValue = document.querySelector('#Select').value;
if (selectValue.length > 0) {
myForm.setAttribute('action', selectValue);
}
}
Just change the file1.php
, file2.php
and file3.php
with your files.
If you don't have a JS file attached to the page, wrap the above JS code inside a <script type="text/javascript"></script>
and place it at the end of the page before the body end tag.
P.S. Now you changed your code putting the name of the file to call directly inside the value of the options. In this way you don't need anymore the switch case, and you can directly use the value of the select as the action attribute of the form to use.
Upvotes: 1