表单字段选项按日期和项目编号更改URL

时间:2018-12-09 23:22:09

标签: javascript php html forms

我正在创建一个包含2个字段的表单-日历和2个产品项的选项。然后需要根据用户在表单中选择的内容来更改此URL。我该怎么做?这是他们转到的网址:

https://example.com/reserve/?date=2018-12-24&item_id=229&popup=1

需要可变的项目是:

date = 2018-12-24 和item_id = 229

我为item_id找到了不错的选择,但也受困于输入日期格式。

这是我当前表单的一个示例,显然不能正常工作:

<form method="get" action="http://example.com/url1">
<input type="date" name="bday">
   <select name="_" onchange="this.form.action=this.value">
     <option value="http://example.com/url1">Item 1</option>
     <option value="http://example.com/url2">Item 2</option>
   </select>

   <input type="submit" value="Submit" />
</form>

1 个答案:

答案 0 :(得分:1)

我假设您不希望将所有表单值作为查询字符串发送。在这种情况下,您可以尝试取消原始的提交事件并使用查询字符串重定向到所选页面。参见How do I redirect to another webpage?

    <?php
$fromemail = ".$email.";
$name = ".$name.";
$title = ".$title.";
$subject="Uploaded file attachment";  
$body="Please find the attachment";
$strSid = md5(uniqid(time()));
$headers ="From: $name" ."\n";
$headers ="From: $fromemail" . "\n";
$headers ="Title: $title". "\n";
$headers .="MIME-Version: 1.0\n";
$headers .="Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n"; 
$headers .="--".$strSid."\n";  
$headers .="Content-type: text/html; charset=utf-8\n";  
$headers .="Content-Transfer-Encoding: 7bit\n\n";  
$headers .=$body."\n";
for($i=0;$i<count($_FILES['file']['name']);$i++){
  if($_FILES["file"]["name"][$i]!= ""){  
    $strFilesName = $_FILES["file"]["name"][$i];  
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"][$i])));  
    $headers .= "--".$strSid."\n";  
    $headers .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
    $headers .= "Content-Transfer-Encoding: base64\n";  
    $headers .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
    $headers .= $strContent."\n\n";  
  }
}
$toemail="azubuinedaniel05@gmail.com";  
if(mail($toemail, $subject, $body, $headers))
   echo "Email send successfully with attachment";
?>

结果:

<form method="get" id="myForm" action="http://example.com/url1"> <input type="date" name="bday"> <select name="actionUrl"> <option value="http://example.com/url1">Item 1</option> <option value="http://example.com/url2">Item 2</option> </select> <input type="submit" value="Submit" /> </form> <script type="text/javascript"> $(document).ready(function(){ $("#myForm").on('submit', function(e){ e.preventDefault(); var bday = $(this).find('input[name="bday"]').val(); var url = $(this).find('select[name="actionUrl"]').val(); url += "?bday="+bday; window.location.replace(url); }); }); </script>