简体   繁体   中英

shortcode Conditional Form - PHP

I am attempting to build a simple contact form inside of a function so that I may add it as a shortcode later. I wish to change the mailto address based on a "select" value.

function contact_form_function() {


switch($_POST['selectedValue']){
case 'webmaster':
$mailbox='webmaster@email.com';
break;
case 'careers':
$mailbox='careersk@email.com';

break;
case 'projects':
$mailbox='projects@email.com';
break;
case 'info':
$mailbox='info@hotmail.com';
break;
default:
// Something went wrong or form has been tampered.
}


?>

<form action="MAILTO:<?php echo($mailbox); ?>" method="post" enctype="text/plain">
 <input type="text" name="name" required> Name(required)
 <input type="text" name="mail" required> Mail(will not be published, required)
 <input type="text" name="website"> Website
 <select name="selectedValue">
  <option value="webmaster">Website Comment</option>
  <option value="careers">Careers Information</option>
  <option value="projects">Project Opportunity</option>
  <option value="info">Other</option>
 </select>
<textarea></textarea>

<?php
echo($mailbox);
?>
<input type="submit"></submit>

</form>
<?php
}
?>

Its not echoing anything back and not changing the email address so I know I've done something wrong. I just don't know what. I'm a novice when it comes to coding so any advice is helpful.

You need to set the $mailbox value before you try to use it. I recommend using jQuery.

            <script src="jquery.js"></script>
            <script type="text/javascript">
              jQuery.noConflict();
              (function ($) {
               function readyFn() {
                $("#emailSelect").change(function(){
                 $("#your_form").attr('action', 'MAILTO:' + $("#emailSelect").val() + '@email.com');
                });
               }    
               $(document).ready(readyFn); 
              })(jQuery);
            /*$(document).ready(function(){
               $("#emailSelect").change(function(){
                  $("#your_form").attr('action', 'MAILTO:' + $("#emailSelect").val() + '@email.com');
               });
            });*/
            </script>

        ...

            <form action="" id="yoor_form" method="post" enctype="text/plain">
        ...
            <select name="selectedValue" id="emailSelect">
              <option value="webmaster">Website Comment</option>
              <option value="careersk">Careers Information</option>
              <option value="projects">Project Opportunity</option>
              <option value="info">Other</option>
             </select>
            </form>

You need to set the $mailbox value before you try to use it.

And if you want something to happen in the browser when the user selects an option in the drop down, you have to use javascript, not PHP.

You are defining $mailbox after you try to use the value.

You have to have your $mailbox = ... stuff before your <form> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM