【问题标题】:Ajax Jequery Post without Form tag & without refresh没有表单标签且没有刷新的 Ajax Jquery Post
【发布时间】:2017-04-28 13:44:27
【问题描述】:

我有一个页面可以通过 ajax jquery 发布数据而无需刷新无表单标签。 N 通过小吃店验证。

我知道使用 jQuery 和 AJAX 可以做到这一点,但我对这些语言中的任何一种都没有真正的经验。有人知道如何解决这个问题吗?

这是我的按钮功能:

<input type="text" id="nikpemohon">
<input type="text" id="namapemohon">
<button id="tombolsimpan" onclick="validate()"></button>`

这里是我的 js:

function validate() {
    var regExnamapemohon = /^[a-zA-Z .'-]+$/;

    var values = {
        'nikpemohon': document.getElementById('nikpemohon').value,
        'namapemohon': document.getElementById('namapemohon').value,
    };

    if(document.getElementById('nikpemohon').value == "" || document.getElementById('nikpemohon').value == null || document.getElementById('nikpemohon').value == undefined || document.getElementById('nikpemohon').value.length < 16 ) {
        //get the snackbar
        var notification = document.querySelector('.mdl-js-snackbar');
        //creating data for snackbar notification
        var data = {
            message: 'Isi NIK dengan benar. Contoh: 6108011234567890',
            timeout: 4000
        }
        //pushing the notification to the screen
        notification.MaterialSnackbar.showSnackbar(data);
        document.getElementById('nikpemohon').focus();
    } else if (document.getElementById('namapemohon').value == "" || document.getElementById('namapemohon').value == null || document.getElementById('namapemohon').value == undefined || !regExnamapemohon.test(document.getElementById('namapemohon').value) )  {
        //get the snackbar
        var notification = document.querySelector('.mdl-js-snackbar');
        //creating data for snackbar notification
        var data = {
            message: 'Isi Nama Lengkap hanya dengan huruf abjad saja, tanpa Penulisan Gelar. Contoh: Matius, Markus atau Darius',
            timeout: 4000
        }
        //pushing the notification to the screen
        notification.MaterialSnackbar.showSnackbar(data);
        document.getElementById('namapemohon').focus();
    } else {
        $.ajax({
            url: "ktp_post.php",
            type: "POST",
            data: values,
        }); 
    }
}

这是我的 ktp_post.php:

if(isset($_POST['tombolsimpan'])){ 

$nikpemohon = strtoupper($_POST['nikpemohon']);
$namapemohon = strtoupper($_POST['namapemohon']);

$sql = $con->query("INSERT INTO katimpus (nikpemohon, namapemohon)Values('{$nikpemohon}', '{$namapemohon}')");}

请在此处提供帮助。这个错误花了两天时间。我无法将数据发布到 mysql 数据库..

【问题讨论】:

标签: php jquery ajax mysqli


【解决方案1】:

您是否为按钮使用了正确的值 试试这样:

&lt;form method='post'&gt; &lt;input type="text" id="nikpemohon"&gt; &lt;input type="text" id="namapemohon"&gt; &lt;input type="button" id="tombolsimpan" value="tombolsimpan" onclick="validate()"/&gt; &lt;/form&gt;

【讨论】:

  • 没有。我不使用表单元素。使用表单元素我通常很好。但是,这回合没有使用表单元素。
  • 在你的代码中你正在检查if(isset($_POST['tombolsimpan'])),你没有得到那个值,因为它没有设置......
  • 所以,没有表单元素,我只是删除 php procces 上的 isset?
  • 我你真的想检查isset($_POST['tombolsimpan']) 你必须像这样使用 var values = { 'nikpemohon': document.getElementById('nikpemohon').value, 'namapemohon': document.getElementById( 'namapemohon').value, };
  • 在您的值数组中使用另一个元素,即tombolsimpan: anyValue,以便 issset 方法检查您的帖子数据
【解决方案2】:

我通过删除 .php 进程上的 if(isset($_POST['tombolsimpan'])){} 来修复

【讨论】:

    猜你喜欢
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    相关资源
    最近更新 更多