更改$ _POST内容而不刷新页面?

时间:2015-06-05 13:38:45

标签: php ajax forms post

我有一个包含动态内容的页面和一个用于保存为PDF的按钮。 我通过ajax将内容发送到我的php文件。

问题是我的应用程序永远不会刷新。

如果我更改页面上的内容,然后按保存pdf按钮,$ _POST的内容永远不会更改,直到页面刷新。

有人知道如何避免这种情况吗?

按钮点击进入Jquery事件的Ajax请求。

$(document).on("click", "#saveAll", function(event) {
  event.preventDefault();
  var nombre = 'savedImageData'+Math.random();
  var overviewLoad = '';
  var programsLoad = '';
  var barriersLoad = '';
  var rolesLoad = '';
  var relationsLoad = '';
  var printcont = '';
  $('#printall').html('');
  overviewLoad = $("#orgoverview").html();
  programsLoad = $("#orgprograms").html();
  barriersLoad = $("#orgbarriers").html();
  rolesLoad = $("#orgroles").html();


   var src1 = $("#canvasImg").attr("src");
   relationsLoad = $("#orgrelations").html();
   var rolestoles = $('#orgroles').html();
  $('#printall').append(overviewLoad);
  $('#printall').append('<div class="page">');
  $('#printall').append(programsLoad+'</div>');
  $('#printall').append('<div class="page">');
  $('#printall').append(barriersLoad+'</div>');
  $('#printall').append('<div class="page">');
  $('#printall').append(relationsLoad+'</div>');
  $('#printall').append('<div class="page">');
  $('#printall').append(rolestoles+'</div>');
  printcont = $('#printall').html();



  $('<input>').attr('value', '');
  $('<input>').attr({
    type: 'hidden',
    name: 'contenido',
    id: 'contenido',
    value: printcont 
}).prependTo('form#getPDF');
  $.ajax({
            method: "POST",
            url: "run/runpdf.php?"+Math.random(),
            data: $("form#getPDF").serialize(),
            cache: false,
            headers: { "cache-control": "no-cache" },
            success: function(someMessageFromPhp) {
                mystring = someMessageFromPhp.replace('../','');
                window.open(mystring+'?'+Math.random());
                $('#downloadPDF').hide(); 
            }
        });



  });

Php

    <?php
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
define('DOMPDF_ENABLE_AUTOLOAD', true);

// include DOMPDF's default configuration
require_once 'dompdf/dompdf_config.inc.php';
$contenido .= utf8_encode($_POST['contenido']);
    $dompdf = new DOMPDF();
    $dompdf->set_paper('LETTER', 'portrait');
$dompdf->load_html($contenido);
$dompdf->render();
header('Content-type: application/pdf');
$output = $dompdf->output();
    file_put_contents('Brochure.pdf', $output);
?>

2 个答案:

答案 0 :(得分:0)

@model GRCWebApp.Models.RegisterViewModel

@{
    ViewBag.Title = "Register";
}

<h2 class="text-success">@ViewBag.Title</h2>
<div class="row">
    <div class="col-md-7">
        <div class="well bs-component">
            <form class="form-horizontal">
                <fieldset>
                    <section id="loginForm">
                        @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                        {
                            @Html.AntiForgeryToken()
                            <hr />
                            @Html.ValidationSummary("", new { @class = "text-danger" })

                            <h3 class="text-success col-md-offset-1">Name &amp; Address</h3>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-3  col-md-offset-1">
                                        @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control", placeholder = "John" } })
                                        @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                                    </div>
                                    <div class="col-md-4 col-md-offset-2">
                                        @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control", placeholder = "Smith" } })
                                        @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-5 col-md-offset-1">
                                        @Html.LabelFor(model => model.Address1, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.Address1, new { htmlAttributes = new { @class = "form-control", placeholder = "1 Apple Road" } })
                                        @Html.ValidationMessageFor(model => model.Address1, "", new { @class = "text-danger" })
                                    </div>
                                    <div class="col-md-5">
                                        @Html.LabelFor(model => model.Address2, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.Address2, new { htmlAttributes = new { @class = "form-control", placeholder = "Neighbourhood" } })
                                        @Html.ValidationMessageFor(model => model.Address2, "", new { @class = "text-danger" })
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-md-5 col-md-offset-1">
                                        @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control", placeholder = "Some Town" } })
                                        @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
                                    </div>
                                    <div class="col-md-5">
                                        @Html.LabelFor(model => model.County, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.County, new { htmlAttributes = new { @class = "form-control", placeholder = "Someshire" } })
                                        @Html.ValidationMessageFor(model => model.County, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-3 col-md-offset-1">
                                        @Html.LabelFor(model => model.Postcode, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.Postcode, new { htmlAttributes = new { @class = "form-control", placeholder = "AA1 2BB" } })
                                        @Html.ValidationMessageFor(model => model.Postcode, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                            <h3 class="text-success col-md-offset-1">Contact Details</h3>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-4 col-md-offset-1">
                                        @Html.LabelFor(model => model.Telephone, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { @class = "form-control", placeholder = "01234 567890" } })
                                        @Html.ValidationMessageFor(model => model.Telephone, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-4 col-md-offset-1">
                                        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", placeholder = "me@provider.com" } })
                                        @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
                                    </div>
                                    <div class="col-md-4 col-md-offset-1">
                                        @Html.LabelFor(model => model.ConfirmEmail, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.ConfirmEmail, new { htmlAttributes = new { @class = "form-control", placeholder = "me@provider.com" } })
                                        @Html.ValidationMessageFor(model => model.ConfirmEmail, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                            <h3 class="text-success col-md-offset-1">Competition Licence</h3>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-4 col-md-offset-1">
                                        @Html.LabelFor(model => model.CompLicenceNo, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.CompLicenceNo, new { htmlAttributes = new { @class = "form-control", placeholder = "123456" } })
                                        @Html.ValidationMessageFor(model => model.CompLicenceNo, "", new { @class = "text-danger" })
                                    </div>
                                    <div class="col-md-3 col-md-offset-1">
                                        @Html.LabelFor(model => model.SelectedLicenceTypeId, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.SelectedLicenceTypeId, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.SelectedLicenceTypeId, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                            <h3 class="text-success col-md-offset-1">Personal Details</h3>
                            <h4 class="text-success col-md-offset-1">Why do we need this information?</h4>
                            <p>To make it easier for you to enter events here on Grass Roots Clicks we gatther certain information that we can then populate into your entry. Why do we need a date of birth? Organisers gain great benefit form knowing what ages are taking part in their events. We dont share your date of birth, we use your current age to help the organisers and also tailor the entry form to you e.g. if your under 18 we'll ask you for parental permission for some events.</p>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-3 col-md-offset-1">
                                        @Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control", placeholder = "01/12/80" } })
                                        @Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-4 col-md-offset-1">
                                        @Html.LabelFor(model => model.NOKFirstName, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.NOKFirstName, new { htmlAttributes = new { @class = "form-control", placeholder = "Jane" } })
                                        @Html.ValidationMessageFor(model => model.NOKFirstName, "", new { @class = "text-danger" })
                                    </div>
                                    <div class="col-md-4 col-md-offset-1">
                                        @Html.LabelFor(model => model.NOKLastName, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.NOKLastName, new { htmlAttributes = new { @class = "form-control", placeholder = "Smith" } })
                                        @Html.ValidationMessageFor(model => model.NOKLastName, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-4 col-md-offset-1">
                                        @Html.LabelFor(model => model.NOKTelephone, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.NOKTelephone, new { htmlAttributes = new { @class = "form-control", placeholder = "07234 567890" } })
                                        @Html.ValidationMessageFor(model => model.NOKTelephone, "", new { @class = "text-danger" })
                                    </div>
                                    <div class="col-md-4 col-md-offset-1">
                                        @Html.LabelFor(model => model.RelationshipTypeId, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.RelationshipTypeId, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.RelationshipTypeId, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-7 col-md-offset-1">
                                        @Html.LabelFor(model => model.Dietary, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.Dietary, new { htmlAttributes = new { @class = "form-control", placeholder = "Vegetarian" } })
                                        @Html.ValidationMessageFor(model => model.Dietary, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                            <h3 class="text-success col-md-offset-1">Password</h3>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-4 col-md-offset-1">
                                        @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
                                    </div>
                                    <div class="col-md-4 col-md-offset-1">
                                        @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label" })
                                        @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                            <h3 class="text-success col-md-offset-1">Contact</h3>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-9 col-md-offset-1">
                                        @Html.LabelFor(model => model.OtherOrgsGRC, htmlAttributes: new { @class = "control-label" })
                                    </div>
                                    <div class="col-md-1">
                                        @Html.CheckBoxFor(model => model.OtherOrgsGRC, new { @checked = "checked" })
                                        @Html.ValidationMessageFor(model => model.OtherOrgsGRC, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-9 col-md-offset-1">
                                        @Html.LabelFor(model => model.OtherClubEvents, htmlAttributes: new { @class = "control-label" })
                                    </div>
                                    <div class="col-md-1">
                                        @Html.CheckBoxFor(model => model.OtherClubEvents, new { @checked = "checked" })
                                        @Html.ValidationMessageFor(model => model.OtherClubEvents, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-9 col-md-offset-1">
                                        @Html.LabelFor(model => model.OtherOrgsOutside, htmlAttributes: new { @class = "control-label" })
                                    </div>
                                    <div class="col-md-1">
                                        @Html.CheckBoxFor(model => model.OtherOrgsOutside, new { @checked = "checked" })
                                        @Html.ValidationMessageFor(model => model.OtherOrgsOutside, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-10 col-md-offset-1">
                                    <input type="submit" value="Register" class="btn btn-success btn-lg" />
                                </div>
                            </div>
                        }
                    </section>
                </fieldset>
            </form>
        </div>
    </div>


    <div class="col-md-4 panel panel-success">
        <div class="panel-heading">
            <h3 class="panel-title " align="center">Use another service to register</h3>
        </div>

        @Html.Partial("_ExternalLoginsListPartial", new GRCWebApp.Models.ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })

    </div>
</div>


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/bootstrap")
<script type="text/javascript">
    $("#DateOfBirth").datepicker({
        format: "dd/mm/yyyy",
        startDate: "-120y",
        endDate: "-10y",
        startView: 2,
        calendarWeeks: true,
        defaultViewDate: { year: 1975, month: 01, day: 01 }
    });
    </script>
    }

当您的ajax调用的结果发送到客户端时,将调用success方法。这应该独自工作。如果您想强制刷新,请在成功回调中使用$.ajax({ url: 'yourUrl', success: function(data){ yourHTMLInputsOrWhatEver = data; } });

答案 1 :(得分:0)

解决: 我没有通过POST表单输入内容,而是通过ajax传递一个带有加载html的变量来传递。

Parent

感谢您的时间,感谢您的帮助!