简体   繁体   English

PHP JQUERY AJAX表单提交响应

[英]PHP JQUERY AJAX FORM Submit Response

I have a very detailed form built with php when i try to do the regular GET submit with php it works just fine, but i need to do this with ajax as i need to make a live preview with some information from the php file, I am familiar with PHP, HTML, CSS but i have no experience with ajax 当我尝试使用php进行常规GET提交时,我使用php构建了一个非常详细的表格,它工作正常,但是我需要使用ajax进行此操作,因为我需要使用php文件中的一些信息进行实时预览,我熟悉PHP,HTML,CSS,但我没有使用Ajax的经验

So my form is like this and it has two submit buttons i am concerned about the preview button. 所以我的表单是这样的,它有两个提交按钮,我担心预览按钮。 Basically i want the page to submit to the php dopdf.php and bring back the results from it. 基本上,我希望页面提交到php dopdf.php并从中带回结果。

<form action="dopdf.php" name="formular" id="form" method="GET" enctype="multipart/form-data">
<!-- Lots of input -->
<input type="submit" value="Create PDF" name="print" class="btn btn-primary submit-button" onclick="javascript:document.getElementById('act').value='0'" style="margin-left: 0;" />
<input type="submit" value="Preview!" name="preview" class="btn btn-primary submit-button" onclick="javascript:document.getElementById('act').value='0'" style="margin-left: 0;" />

And this here is the jquery i am using to sumbit via ajax: 这是我用来通过ajax求和的jquery:

<script>
$('#form').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
    data: $(this).serialize(), // get the form data
    type: $(this).attr('GET'), // GET or POST
    url: $(this).attr('dopdf.php'), // the file to call
    success: function(response) { // on success..
    //    $('#created').append(response); // update the DIV
    alert(response);
    }
});
return false; // cancel original event to prevent form submitting
});
</script>

and this here is the code as a test that i am trying if this works i'll put in the data in the if statement: 这是作为测试的代码,如果可以使用,我会尝试将其放入if语句中的数据:

if (isset($_GET['preview'])) { ?>
   <?php echo = "This is a test";  exit();?>
<?php } else {
   // This is for the other submit button.
}

The Issue that i have is the ajax works and i get a response in the alert but the alert response that i get is the same page that i posting from since i am posting this form from document.php so i am getting document.php back. 我遇到的问题是ajax可以工作,我在警报中得到响应,但是我得到的警报响应与我从中发布的页面相同,因为我从document.php发布了此表单,所以我得到了document.php 。

You have a mistake when setting the url. 设置网址时出错。 The following should be correct: 以下应该是正确的:

$(this).attr('action')

I think you are actually doing things twice. 我认为您实际上在做两次事情。 When you press the button it is running the javascript but also processing the submit as it always did. 当您按下按钮时,它会运行javascript,而且会像往常一样处理提交。

You need to tell the browser not to do the normal submit as you are now doing it with js/Ajax so add this line to your javascript 您需要告诉浏览器不要像现在使用js / Ajax一样进行正常的提交,因此请将此行添加到javascript中

Also you have a few error in setting the ajax parameters. 另外,在设置ajax参数时会出现一些错误。

Use 采用

<script type="text/javascript">
$('#form').submit(function(event) { // catch the form's submit event
    event.preventDefault();

    $.ajax({ // create an AJAX call...

        data: $(this).serialize(), // get the form data
        type: $(this).attr('method'), // GET or POST
        url: $(this).attr('action'), // the file to call
        success: function(response) { // on success..
        //    $('#created').append(response); // update the DIV
        alert(response);
        }
    });

});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM