简体   繁体   English

使用AJAX的带有参数的PHP POST不起作用

[英]PHP POST with parameters using AJAX not working

I'm using AJAX to call a PHP script, and passing some parameters. 我正在使用AJAX调用PHP脚本,并传递一些参数。

The PHP script is creating a PDF using FPDF. PHP脚本正在使用FPDF创建PDF。 The values that I'm passing to the PHP script should only be used to concatenate to another string within the script. 我传递给PHP脚本的值仅应用于连接脚本中的另一个字符串。

The AJAX is calling the PHP script and the PDF is generating as it should, but the only problem is that the parameters coming up as empty. AJAX正在调用PHP脚本,而PDF正在按照其应有的方式生成,但是唯一的问题是参数显示为空。 I'm not sure what I'm doing wrong and it is driving me crazy... 我不确定自己在做什么错,这让我发疯了...

Here is my HTML : 这是我的HTML:

<input type="submit" value="Download Completed DA190" onclick="printCompletedForm()" />
<script type="text/javascript">

function printCompletedForm()
{
    var testDate = "2012-08-19";

    $.ajax({  
        type: "POST",  
        url: "saveZeroRelease.php",
        data: {testdate : testDate},
        success: function(msg){

        },
        error: function(msg){

        }
    });
}

</script>

Here is my PHP : 这是我的PHP:

$testDate = $_POST['testdate'];


$pdf = new PDF_MC_Table();
$pdf->Open();
$pdf->AddPage();
$pdf->SetFont('Arial','',8);
$pdf->Cell(150,10,'Certificate of foreign Currency usage in respect of materials and components in terms of the notes to rebate item ',1);
$pdf->SetFont('Arial','',10);
$pdf->Cell(40,10,'Form C1',1);
$pdf->Ln(10);
$pdf->Cell(190,10,'NOTE:',1);
$pdf->Ln(10);
$pdf->MultiCell(190,10,'Certificate Number:      Effective Date :'.$testDate.'  
Replaces Certificate No:_________________                                                        Effective Date :________________',1);
$pdf->Output();

I don't know what I'm doing wrong. 我不知道我在做什么错。 Any help please guys! 任何帮助请大家!

First, in your JavaScript, wrap your code inside; 首先,在JavaScript中,将代码包装在其中;

$(document).ready( function() {
//your code here
});

And don't forget to link your jQuery file like; 并且不要忘记像这样链接您的jQuery文件;

<script type="text/javascript" src="jquery.js"></script>

Also, in your php, change; 另外,在您的php中,进行更改;

$testDate = $_POST['testdate'];

to

$testDate = $_POST["testdate"];

Hope this helps. 希望这可以帮助。

It looks like the default behavior of submitting the form is triggered and executed before your AJAX call has time to do everything you're asking it to. 在您的AJAX调用有时间完成您要求的所有操作之前,似乎已触发并执行了提交表单的默认行为。 Look into event.preventDefault() to override the submit handler entirely. 查看event.preventDefault()以完全覆盖提交处理程序。 When the form is submitted, your function will then handle everything from making the $.ajax POST, and then redirecting the user as necessary. 提交表单后,您的函数将处理所有事情,包括进行$ .ajax POST,然后根据需要重定向用户。

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

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