[英]Passing form data from view to controller from a form
我是laravel的新手,正在從事現有項目。 我有一個將數據傳遞給Laravel控制器的表格:
<div class="share_popup" id="share_popup">
<div class="row">
<div class="col">
<div class="body">
<button class="x">
<span class="sprite close"></span>
</button>
<div class="sprite logo"></div>
<!--<strong><span id="item_shared">Item name</span></strong>-->
<form id="email_form">
<input name="share_email" id="share_email" type="text" class="email" placeholder="Email" required>
<input name="share_name" id="share_name" type="text" class="name" placeholder="Name" required>
<input type="hidden" name="hint_id" id="hint_id" value="">
<span style="float:left;">Include a message below, if you'd like. test2</span><br/><br/>
<textarea name="message" id="message" placeholder="Message here..." class="description"> </textarea>
<button type="submit" onclick="shareHint(event)" id="share_button">Share</button>
</form>
</div>
</div>
</div>
</div>
這是在提交時在控制器中運行的函數的簡化版本:
function shareHint($hint_id) {
$input = \Request::input();
$passedemail = $input['email'];
}
$ input ['email']如何獲取電子郵件地址:
<input name="share_email" id="share_email" type="text" class="email" placeholder="Email" required>
從表格的這一部分傳遞過來的?
我看不到關聯來自何處,但它正在過去。
如果我想要另一個表單字段,我將如何作為控制器功能的一部分來訪問它?
編輯:
我相信Moppo是正確的,因為此JavaScript正在運行。
function shareHint(e, hint_id){
e.preventDefault();
var email = $("#share_email").val();
var hinturl = window.location.protocol + "//" + window.location.host + "" + window.location.pathname + "?h="+data.currentHint._id;
var r = new XMLHttpRequest;
r.open("POST", "/hint/"+ hint_id +"/share"), r.addEventListener("readystatechange", function() {
if (4 == r.readyState)
if (200 == r.status){
$(".share_popup").css('visibility', 'hidden');
$("#share_email").val("");
$("#message").val("");
//alert("Successfully shared hint with " + email + "!")
$("#alert_error").html("Successfully shared hint with " + email + "!");
$("#alert_popup").addClass("visible");
}else if (400 == r.status) {
var e = JSON.parse(r.responseText);
//alert(e.message)
$("#alert_error").html(e.message);
$("#alert_popup").addClass("visible");
}
else{
$("#alert_error").html('Sorry, an error occurred');
$("#alert_popup").addClass("visible");
// alert("Sorry, an error occurred")
}
});
var a = new FormData;
a.append("hinturl", hinturl), a.append("email", email), a.append("message", $("#message").val()),a.append("_token", csrf_token), r.send(a)
}
如果名為share_email
的輸入作為email
到您的控制器,則可能是某些中間件修改了請求,從而更改了輸入名稱。 因此,您應該檢查該應用程序的中間件,看看其中是否正在更改請求。
或者,可能是某些javascript代碼在發送表單之前更改了輸入名稱
編輯
查看該代碼,實際上是javscript發送“ email”輸入:
var a = new FormData;
a.append("hinturl", hinturl), a.append("email", email),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.