第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ajax和php輸入多個表單輸入到數(shù)據(jù)庫

ajax和php輸入多個表單輸入到數(shù)據(jù)庫

PHP
喵喵時光機 2019-07-27 19:55:53
ajax和php輸入多個表單輸入到數(shù)據(jù)庫我有一個PHP生成的表單,其中包含多個輸入字段,其數(shù)量由用戶選擇確定。我想使用ajax函數(shù)將所有數(shù)據(jù)輸入數(shù)據(jù)庫。問題是我是ajax的新手,并不確定如何去做。下面的ajax javascript函數(shù)是我想要實現(xiàn)的一個示例,我知道它不正確。有人能指出我正確的方向。我環(huán)顧四周,從我看到的情況來看,Json可能是一個解決方案,但我對它一無所知并且閱讀它我仍然沒有得到它。樣本ajax:function MyFunction(){var i = 1;var x = $('#num_to_enter').val();    while (i <= x){     var name = $('#fname[i]').val();     var lname = $('#lname[i]').val();     var email = $('#Email[i]').val();     i++;  }     $('#SuccessDiv').html('Entering Info.<img src="images/processing.gif" />');      $.ajax({url : 'process.php',     type:"POST",    while (i <= x){     data: "fname[i]=" + name[i] + "&lname[i]=" + lname[i] + "&email[i]=" + email[i],     i++;  }      success : function(data){     window.setTimeout(function()     {     $('#SuccessDiv').html('Info Added!');     $('#data').css("display","block");     $('#data').html(data);     }, 2000);         }});         return false;           }表格樣本:<?php    echo "<form method='post'>";    $i=1;     while($i <= $num_to_enter){     $form_output .= "First Name:     <input id='fname' type='text' name='fname[$i]'><br />      Last Name:     <input id='lname' type='text' name='lname[$i]'><br />     Email:     <input id='Email' type='text' name='Email[$i]'><br />     $i++;    }    echo"<input type='button' value='SUBMIT' onClick='MyFunction()'></form>";   ?>Then DB MySQL Sample   <?php       while ($i <= $x){     $x = $_POST['num_to_enter'];     $fname = $_POST['fname[$i]'];     $fname = $_POST['fname[$i]'];     $fname = $_POST['email[$i]'];         $sql = "INSERT INTO `mytable`      (`firstname`, `lastname`, `email`) VALUES ('$fname[$i]', '$lname[$i]', '$email[$i]');";     $i++;     }    ?>
查看完整描述

2 回答

?
千巷貓影

TA貢獻1829條經(jīng)驗 獲得超7個贊


這是一個簡單的AJAX演示:


HTML


<form method="POST" action="process.php" id="my_form">

    <input type="text" name="firstname[]">

    <input type="text" name="firstname[]">

    <input type="text" name="firstname[]">

    <input type="text" name="firstname[custom1]">

    <input type="text" name="firstname[custom2]">

    <br><br>

    <input type="submit" value="Submit">

</form>

jQuery的


// listen for user to SUBMIT the form

$(document).on('submit', '#my_form', function(e){


    // do not allow native browser submit process to proceed

    e.preventDefault();


    // AJAX yay!

    $.ajax({

        url: $(this).attr('action') // <- find process.php from action attribute

        ,async: true // <- don't hold things up

        ,cache: false // <- don't let cache issues haunt you

        ,type: $(this).attr('method') // <- find POST from method attribute

        ,data: $(this).serialize() // <- create the object to be POSTed to process.php

        ,dataType: 'json' // <- we expect JSON from the PHP file

        ,success: function(data){


            // Server responded with a 200 code


            // data is a JSON object so treat it as such

            // un-comment below for debuggin goodness

            // console.log(data);


            if(data.success == 'yes'){

                alert('yay!');

            }

            else{

                alert('insert failed!');

            }

        }

        ,error: function(){

            // There was an error such as the server returning a 404 or 500

            // or maybe the URL is not reachable

        }

        ,complete: function(){

            // Always perform this action after success() and error()

            // have been called

        }

    });

});

PHP process.php


<?php

/**************************************************/

/* Uncommenting in here will break the AJAX call */

/* Don't use AJAX and just submit the form normally to see this in action */


// see all your POST data

// echo '<pre>'.print_r($_POST, true).'</pre>';


// see the first names only

// echo $_POST['firstname'][0];

// echo $_POST['firstname'][1];

// echo $_POST['firstname'][2];

// echo $_POST['firstname']['custom1'];

// echo $_POST['firstname']['custom2'];


/**************************************************/


// some logic for sql insert, you can do this part


if($sql_logic == 'success'){


    // give JSON back to AJAX call

    echo json_encode(array('success'=>'yes'));

}

else{


    // give JSON back to AJAX call

    echo json_encode(array('success'=>'no'));

}

?>


查看完整回答
反對 回復 2019-07-27
?
紅糖糍粑

TA貢獻1815條經(jīng)驗 獲得超6個贊

var postdata={};postdata['num']=x;
   while (i <= x){
    postdata['fname'+i]= name[i];
    postdata['lname'+i]= lname[i];
    postdata['email'+i]= email[i];

    i++;}
 $.ajax({url : 'process.php',
    type:"POST",
    data:postdata,
    success : function(data){
    window.setTimeout(function()
    {
    $('#SuccessDiv').html('Info Added!');
    $('#data').css("display","block");
    $('#data').html(data);
    }, 2000);
        }});

PHP

$num=$_POST['num'];for($i=1;i<=$num;i++){
 echo $_POST['fname'.$i];
 echo $_POST['lname'.$i];
 echo $_POST['email'.$i];}


查看完整回答
反對 回復 2019-07-27
  • 2 回答
  • 0 關(guān)注
  • 679 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號