通过AJAX/PHP设置变量提交URL,而无需刷新页面,加载变量 [英] Submit URL via AJAX / PHP set variable without refreshing page, load variable
问题描述
我遇到一些脚本问题,这些脚本无法通过AJAX执行功能而不刷新我的页面.我有一个供用户输入外部URL的字段,当他们单击提交"时,它将弹出一个模态窗口,其中包含通过单独的PHP页面(当前为images.php)生成的一些信息.当表单实际提交,页面重新加载并且images.php能够看到index.php?url =任何内容时,我都有脚本在工作,但是我试图更新页面而不必刷新.定义变量后是否需要重新渲染DIV?我认为这可能是我遇到问题的地方.
I'm having an issue with some script to perform a function via AJAX without refreshing my page. I have a field for a user to enter an external URL, and when they click submit it pops up a modal window, with some information generated through a separate PHP page (images.php currently). I have the script working when the form is actually submitted, the page reloads, and images.php is able to see index.php?url=whatever, but I'm trying to update the page without having to refresh. Do I need to re-render the DIV after defining the variable? I think this may be where I'm having problems.
JS
<script type="text/javascript">
$(function() {
$("#newNote").submit(function() {
var url = "images.php"; // the script where you handle the form input.
var noteUrl = $('#noteUrl).val();
$.ajax({
type: "POST",
url: url,
data: {noteUrl: noteUrl},
success: function(data)
{
alert(data); // show response from the php script.
}
});
return false; // avoid to execute the actual submit of the form.
});
});
</script>
HTML
<form id="newNote">
<input type="text" class="form-control" id="noteUrl">
<input type="button" class="btn btn-default" id="addNote" data-toggle="modal" data-target="#noteModal" value="Add Note"/>
</form>
PHP(除了要提交给它的表单,它也包含在模式中,该模式将打开,但在var_dump($ postUrl)上返回NULL)
PHP (aside from form being submitted to this, this is also included in the modal, which opens, but returns NULL on var_dump($postUrl))
$postUrl = $_REQUEST['noteUrl'];
echo $postUrl;
我肯定会在这里遗漏一些明显的东西,但是说实话,我已经尝试过在这里可以找到的AJAX示例的每种组合.我是否错过了让PHP获取变量的重要步骤?我需要在某个地方刷新DIV吗?
I could definitely be missing something glaring here, but honestly I've tried every combination of AJAX example I could find on here. Am I missing a huge step about having PHP get the variable? Do I need to refresh a DIV somewhere?
请帮助.
推荐答案
以下是同一代码的精巧版本,已纠正了缺少的引号.
Here is a bit neater version of the same code, with the missing quote corrected.
$(function() {
$("#newNote").submit(function() {
$('#notePreview').empty();
var url = "images.php"; // the script where you handle the form input.
var noteUrl = $(this).find('#noteUrl').val();
var request = $.ajax({
type: "POST",
url: url,
data: {noteUrl: noteUrl}
});
request.done(function(data) {
$('#notePreview').append(data);
});
return false; // avoid to execute the actual submit of the form.
});
});
这篇关于通过AJAX/PHP设置变量提交URL,而无需刷新页面,加载变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!