【发布时间】:2019-07-22 13:03:38
【问题描述】:
我有一个带有多个按钮和一个提交按钮的 HTML 表单。我正在尝试传递在单击提交按钮之前单击的按钮的值。该程序的主要功能是在相应的表中上传一个与按钮同名的 csv 文件,即表的名称与按钮的值相同。所以传入的值会在后续php页面的mysql查询中使用。
我用这样的php post方法试过了:
if(isset($_POST['submit'])){
$_POST['buttonname'];
}
index.php
<form id="upload_csv" method="GET" enctype="multipart/form-data">
<button type="button" id="button1" name = "button1"
value="role">Role</button>
<button type="button" id="button2" name = "button1"
value="report">Report</button>
<button type="button" id="button3" name = "button3">Brand</button>
<!-- uploading the csv file here -->
<input type="file" name="employee_file" style="margin-top:15px;" />
<input type = "submit" value="submit" name="submit">
</form>
<!-- ajax code to upload csv -->
<script>
$(document).ready(function(){
$('#button1,#button2,#button3').click(function(){
$('#upload_csv').on("submit", function(e){
e.preventDefault();
$.ajax({
url:"import.php",
method:"POST",
data:new FormData(this),
contentType:false,
cache:false,
processData:false,
success: function(data){
alert("Data updated successfully");
}
})
});
});
});
</script>
因此,首先,提交按钮只有在其中一个按钮被点击时才应该发布。如果单击按钮然后单击提交按钮,则单击按钮的值将传递到下一页。例如,如果单击第一个按钮,则应传递值“role”。
【问题讨论】:
-
你需要一些 AJAX。
-
使用按钮作为单选元素?样式单选按钮的标签看起来像一个按钮。
标签: javascript php jquery html ajax