我创建了下面的表单,当按下提交按钮时,假设发出一个帖子请求,并在php中添加到mysql。但是,当执行发布请求时,我一直收到console.log(process.argv);
。
形式
Post 500 (Internal Server Error)
的javascript
<form method="post" action="">
<div class="reg_section personal_info">
<input type="text" id="title" value="" placeholder="Campnavn" required="required" maxlength="25">
<textarea name="textarea" id="description" value="" placeholder="Beskrivelse" required="required" minlength="100"></textarea>
<div class="upload">
<input type="file" name="upload" accept="image/*" id="image" />
<span class="fileName">Select file..</span>
</div>
</div>
<div>
<span class="submit" style="text-align: left; padding: 0 10px;"><input type="submit" id="insert" value="Tilføj"></span>
<span class="submit" style="text-align: right; padding: 0 10px;"><input TYPE="button" value="Fortryd" onclick="div_hide();"></span>
</div>
</form>
insert.php
$( document ).ready(function() {
$("#insert").click(function(e) {
var description = $('#description').val();
var title = $('#title').val();
var image = $("#image").serialize()
$.post("insert.php",
{
title: title,
body: description,
longitude: currentMarker.lng(),
latitude: currentMarker.lat(),
image: image
}, function (data) {
}).error(function () {
});
});
});
答案 0 :(得分:1)
请试试这个,我更改<form>
标签我添加了ID和名称,
并将提交按钮更改为普通按钮
<button type="button" onclick="postdata();" id="insert" >Tilføj</button>
在我将onclick函数定义为按钮后...
<强>形式强>
<form id="FormData" name="FormData">
<div class="reg_section personal_info">
<input type="text" id="title" value="" placeholder="Campnavn" required="required" maxlength="25">
<textarea name="textarea" id="description" value="" placeholder="Beskrivelse" required="required" minlength="100"></textarea>
<div class="upload">
<input type="file" name="upload" accept="image/*" id="image" />
<span class="fileName">Select file..</span>
</div>
</div>
<div>
<span class="submit" style="text-align: left; padding: 0 10px;">
<button type="button" onclick="postdata();" id="insert" >Tilføj</button>
</span>
<span class="submit" style="text-align: right; padding: 0 10px;">
<input TYPE="button" value="Fortryd" onclick="div_hide();"></span><br><br>
<span id="result" style="color:#9B0E11; font-size:13px; font-weight:700"></span>
</div>
</form>
的JavaScript
<script type="text/javascript">
function postdata() {
var description = $('#description').val();
var title = $('#title').val();
var image = $("#image").serialize();
$.ajax({
type: 'POST',
url: 'insert.php',
data: {
desc: desctiption,
title: title,
image:image,
longitude: currentMarker.lng(),
longitude: currentMarker.lat(),
},
success: function (answer) {
$("#result").html(answer) // if you wanna get response from insert.php
$("#FormData")[0].reset(); // if you wanna reset form after submit
}
})
}
</script>
PS:在insert.php中你在if语句的末尾加上一个“)”你必须删除它
if (isset($_POST['title']) && isset($_POST['body']) && isset($_POST['longitude']) && isset($_POST['latitude'])){