通过AJAX

时间:2017-12-04 10:13:44

标签: javascript php ajax post

我有以下表格,然后我通过AJAX提交,通过Ajax插入MySQL DB。表单中的所有输入框都有自己的Id,我将它们全部放在我的.php进程文件中,除了" cas"和" dat"似乎没有通过AJAX发布过程。

表格:

<form id="form">
    <div class="form-group">
        <label class="lab" for="nm">id</label>
        <input disabled type="text" id="id" name="id" class="form-control" placeholder="Id">
    </div>

    <div class="form-group">
        <input type="text" class="form-control" name="cas" id="cas" value="2">
        <input type="text" class="form-control" name="dat" id="dat" value="2017-11-30">
    </div>

    <div class="form-group">
        <label class="lab" for="nm">Product</label> <?php 
        //// function populate ($sql, $class,$name, $id, $title, $value,$option)

        echo populate ("SELECT * FROM product_family order by product_type_id ASC","form-control","nm","nm","Select Product", "product_family", "product_family");?>
    </div>

    <div class="form-group">
        <label class="lab" for="em">Win</label>
        <input type="text" id="em" name="em" class="form-control allow_decimal" placeholder="Win">
    </div>

    <div class="form-group">
        <label class="lab" for="hp">Drop</label>
        <input type="text" id="hp" name="hp" class="form-control allow_decimal" placeholder="Drop">
    </div>

    <div class="form-group">
        <label class="lab" for="ad">Currency</label> <?php 
        //// function populate ($sql, $class,$name, $id, $title, $value,$option)

        echo populate ("SELECT * FROM currency order by id ASC","form-control","ad","ad","Select Currency", "currency", "currency");?>
    </div>
    <button type="button" id="save" class="btn btn-success" onclick="saveData()">Save</button>
    <button type="button" id="update" class="btn btn-warning" onclick="updateData()">Update</button>
</form>

然后我有以下JavaScript代码触发插入&#34;保存数据&#34;单击以将不同的输入框值发布到我的.php处理文件:

function saveData(){
    var id = $('#id').val();
    var name = $('#nm').val();
    var email = $('#em').val();
    var phone = $('#hp').val();
    var address = $('#ad').val();
    var casino = $("#cas").val()
    var date = $("#dat").val();

    $.post('server.php?p=add', {id:id, nm:name, em:email, hp:phone, ad:address, cas:casino, dat:date}, function(data){
        viewData()
        $('#id').val(' ')
        $('#nm').val(' ')
        $('#em').val(' ')
        $('#hp').val(' ')
        $('#ad').val(' ')

    })
}

function viewData(){
    $.get('server.php', function(data){
        $('tbody').html(data)
    })
}

然后我尝试阅读我的&#34; $ _ post&#34; PHP方面的值:

if($page=='add'){
    try{
        $id = $_POST['id'];
        $nm = $_POST['nm'];
        $em = $_POST['em'];
        $hp = $_POST['hp'];
        $ad = $_POST['ad'];        
        $casino_id =  $_POST['cas'];
        $date =  $_POST['dat'];       
 }

我完全得到除$ _post列表中没有出现的dat和cas帖子之外的所有变量。按以下方式列出所有$ _Post:

$myfile = fopen("LOGPOST.txt", "w") or die("Unable to open file!");
        foreach ($_POST as $key => $value){
        $txt= $txt."{$key} = {$value}//";

提供以下输出:id = //nm = F&B Sales//em = 1000//hp = 500//ad = EUR//

我做错了什么?

0 个答案:

没有答案