没有将数据添加到数据库中,也没有在使用AJAX和PHP的表追加中显示 [英] No added datainto database, and not shown in table append using AJAX and PHP

查看:53
本文介绍了没有将数据添加到数据库中,也没有在使用AJAX和PHP的表追加中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然遇到相同的问题

I am still in the same problem in this link here. So, I used the append method. And here is the AJAX code:

<script>
function addFunction()
{
    var selectW = $('#insert_new').val();
    var selectW = $('#selectW').val();
    var select_at = $('#select_at').val();
    var pay = $('#pay').val();
    var facture = $('#facture').val();
    var select_opt = $('#select_opt').val();

    if(pay!="")
    {
        $.ajax({
            data: {'text': insert_new, 'text': selectW, 'text': select_at, 'text': pay, 'text': facture, 'text': select_opt},
            type: "post",
            url: "insert.php",
            success: function(response){
              if(response=="success")
              {
                $('#incident_table').append('<tr><td>'+selectW+'</td><td>'+select_at+'</td><td>'+pay+'</td><td>'+facture+'</td><td>'+select_opt+'</td></tr>');
                $('#selectW').val('');
                $('#select_at').val('');
                $('#pay').val('');
                $('#facture').val('');
                $('#select_opt').val('');
              }
              else
              {
                alert("No data added");
              }
            },
            error: function(){
                alert('error; ' + eval(error));
            }
        });
    }
    else
    {
        alert("Specify a value of payment please!!");
    }
}
</script>

这是我删除action=insert.phpmethod=post并将按钮type=submit转换为我的<form>中的button的形式.形式是:

And here is the form where I removed action=insert.php and method=post and transformed the button type=submit into button in my <form>. So the form is:

    <form name="insertForm" id="form1">
<input type="hidden" name="insert" value="true">
    <tr>
        <td align="center">
          <select id="selectW" name="type">
            <option value="لم يتم التحديد">اختر النوع</option>
            <option value="دولارات">دولارات</option>
            <option value="أيام + دولارات">أيام + دولارات</option>
            <option value="بطاقات">بطاقات</option>
            <option value="هواتف">هواتف</option>
            <option value="اكسسوارات">اكسسوارات</option>
            <option value="تسديد فواتير">تسديد فواتير</option>
          </select>
          <!--<select>
          <?php foreach($result5 as $rows){ ?>
            <option value="<?php echo $rows['item_name'] ?>"><?php echo $rows['item_name'] ?></option>
            <?php } ?>
          </select>-->
          </td>
        <td align="center"><select id="select_at" name="alfa_touch">
            <option value="غير محدد">Not Required</option>
            <option value="Alfa">Alfa</option>
            <option value="Touch">Touch</option></select></td>
        <td align="center"><input type="text" id="pay" name="pay"/></td>
        <td align="center"><input type="text" id="facture" name="facture" placeholder="في حال دفع الفواتير عبر omt"/></td>
        <td align="center"><select id="select_opt" name="currency">
            <option value="9">ليرة</option>
            <option value="10">دولار</option>
            </select></td>

        <td align="center"><input type="button" id="insert_new" onClick="addFunction()" name="insert_new" value="اضافة" />

      </td>

      </tr>
      </form>

这是我的insert.php文件,在我的项目中,我有很长的代码用于不同的提交按钮,但是我只会发布用于此AJAX函数的代码:

Here is my insert.php file, where I have a long codes for different submit buttons in my project, but I will post only the code used for this AJAX function:

    $selectOpt1="";
    if(isset($_REQUEST['insert_new'])){

error_reporting(E_ALL); 
require_once ('../include/global.php');
$selectOpt1 = $_REQUEST['select_opt'];
    if($selectOpt1=="9"){
        $type = $_REQUEST['selectW'];
        $provider = $_REQUEST['select_at'];
        $pay = $_REQUEST['pay'];
        $facture = $_REQUEST['facture'];
        try{
            $query = "INSERT INTO sales
            (type, provider, pay, facture, date_now, time_now) 
            VALUES
            (:type, :provider, :pay, :facture, :date, now())";
            $stmt = $conn->prepare($query);
            $stmt->bindValue(":type", $type);
            $stmt->bindValue(":provider", $provider);
            $stmt->bindValue(":pay", $pay);  
            $stmt->bindValue(":facture", $facture);
            $stmt->bindValue(":date", date("y-m-d"));
            $count = $stmt->execute();
            //header("location: home.php");
            echo "success";
            //$last_id = $conn->lastInsertId();
            //echo $last_id;
        }
        catch(PDOException $e) {
            echo $e->getMessage();
            //header("location: ../pages/insert_false.php?id=".$projid);
            print_r($conn->errorInfo());

        }


    }

    if($selectOpt1=="10"){
        $type = $_REQUEST['type'];
        $provider = $_REQUEST['alfa_touch'];
        $payL = $_REQUEST['pay'];
        $pay = $payL*1500;
        $facture = $_REQUEST['facture'];
        try{
            $query = "INSERT INTO sales
            (type, provider, pay, facture, date_now, time_now) 
            VALUES
            (:type, :provider, :pay, :facture, :date, now())";
            $stmt = $conn->prepare($query);
            $stmt->bindValue(":type", $type);
            $stmt->bindValue(":provider", $provider);
            $stmt->bindValue(":pay", $pay);  
            $stmt->bindValue(":facture", $facture);
            $stmt->bindValue(":date", date("y-m-d"));
            $count = $stmt->execute();
            echo "success";
            //header("location: home.php");

        }
        catch(PDOException $e) {
            echo $e->getMessage();
            //header("location: ../pages/insert_false.php?id=".$projid);
            print_r($conn->errorInfo());

        }


    }
    }

在控制台网络中,我具有这些值:

In console network I have those values:

并且控制台中没有错误.我一直看到警报:没有添加数据.

And no errors in the console. And I kept seeing the alert: No data added.

编辑 我收到以下错误消息:insert_buy.php中未定义的select_opt(我将文件从insert.php更改为insert_buy.php):

EDIT I have gotten this error message: Undefined select_opt in insert_buy.php (I changed the file from insert.php into insert_buy.php):

推荐答案

尝试将ajax中的数据更改为此

Try changing your data in ajax to this

data: {'insert_new': 'insert_new', 'selectW': selectW, 'select_at': select_at, 'pay': pay, 'facture': facture, 'select_opt': select_opt},

您没有得到任何值,因为您的数据变量与$ _request []变量不匹配

You are not getting any value because your data variable does't match with the $_request[] variables

这篇关于没有将数据添加到数据库中,也没有在使用AJAX和PHP的表追加中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆