使用AJAX将值发送到页面,表单操作不起作用 [英] Sending value to a page using AJAX, form action does not work
问题描述
HTML:
< form class =wellaction =lessons / add_lesson.php?user_id =<?php echo $ user_id;?> method =postrole =form>
< div class =form-group>
< input type =textname =titleid =titletabindex =2class =form-controlrequired =true
maxlength =24placeholder = 标题 >
< / div>
< div class =form-group>
< label for =content>内容:< / label>
< / div>
< input type =hiddenname =tokenvalue =<?php echo Token :: generate();?>>
< div class =form-group>
< div class =row>
< div class =col-md-4>< / div>
< div class =col-md-4>
< / div>
< div class =col-md-4>< / div>
< / div>
< / div>
< / form>
AJAX / JQuery
< script>
$(document).ready(function(){
$('form')。submit(function(e){
//保存按钮
var btn_submit = $(这个).find('submit');
e.preventDefault();
tinyMCE.triggerSave();
var content = tinymce.get(内容)。getContent();
$ .ajax({
type:'POST',
url:'lessons / add_lesson.php?user_id =<?php echo $ user_id; 'data':
dataType:'html',
data:{data:'fuck'},
success:function(data){
}
} );
});
});
< / script>
以下是网络响应
您实际上并未发送任何表单数据。 PHP没有收到 添加您的 Good day, I am trying to send data to a page using AJAX, I used ajax because I need the html content of a text area (I used tiny mce plugin text editor), I want to pass the value of the text area and then load it to the page. But then I inspect it and gone to the network XHR, i found out that the page sent the ajax data but its not redirecting to the other page and also my input which has a name of 'title' didn't send the data to the page. Any idea how to fix this? here's my code. Thanks for responding HTML: AJAX/JQuery Here is the network response
You are not actually sending any of your form data. PHP doesn't receive a value for Add your
这篇关于使用AJAX将值发送到页面,表单操作不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! title
的值,因此抱怨(我的猜测) $ _ ['POST'] ['title']
$ b 标题
和内容将code>值赋给
$。ajax
调用中的 data
对象:
$。ajax({
type:'POST',
url:'lessons / add_lesson.php?user_id =< php echo $ user_id;?>',
dataType:'html',
data:{
//添加:
title:$('#title')。 val(),
content:content
},
success:function(data){
}
});
<form class="well" action="lessons/add_lesson.php?user_id=<?php echo $user_id; ?>" method="post" role="form">
<div class="form-group">
<input type="text" name="title" id="title" tabindex="2" class="form-control" required="true"
maxlength="24" placeholder="Title">
</div>
<div class="form-group">
<label for="content">Content:</label>
<textarea id="content" name="content" class="tinymce" placeholder="Content" id="comment"></textarea>
</div>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<div class="form-group">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<input type="submit" name="submit" class="btn btn-primary btn-block" id="update_account" href="#" data-target="updateAccount" value="Create">
</div>
<div class="col-md-4"></div>
</div>
</div>
</form>
<script>
$(document).ready(function(){
$('form').submit(function(e){
//save button
var btn_submit = $(this).find('submit');
e.preventDefault();
tinyMCE.triggerSave();
var content = tinymce.get("content").getContent();
$.ajax({
type: 'POST',
url: 'lessons/add_lesson.php?user_id=<?php echo $user_id; ?>',
dataType: 'html',
data: { data: 'fuck' },
success: function(data){
}
});
});
});
</script>
title
and therefore complains that (my guess) $_['POST']['title']
does not exist.title
and content
values to the data
object in the $.ajax
call:$.ajax({
type: 'POST',
url: 'lessons/add_lesson.php?user_id=<?php echo $user_id; ?>',
dataType: 'html',
data: {
// add this:
title: $('#title').val(),
content: content
},
success: function(data){
}
});