将 HTM 收集发送到数据库

所以我有这个表,当用户在a中输入一些东西时,他们必须单击一个按钮来收集所有内容并将其存储到.contenteditabledivsdivdivsVar OBJ


document.getElementById("done_editing").addEventListener("click", get_values);

var OBJ = [];


function get_values() {

  let divobj = document.querySelectorAll('[contenteditable=true]')

  for (var i = 0; i < divobj.length; i++) {

    OBJ.push(divobj[i].textContent)

    //console.log(OBJ)  

  }


  OBJ = OBJ.filter(item => item)

  console.log(OBJ)

我想将 中的值发送到数据库。我无法绕开如何做到这一点。这不是你的平均形式,所以我想我必须使用ajax,但我没有使用ajax的经验。这就是我对阿贾克斯的。var OBJ


    $.ajax({

    type: "POST",

    url: "test.php",

    data: { arraykey: OBJ },

    success: function (response) {

      alert('succes')

      // You will get response from your PHP page (what you echo or print)

    },

    error: function (jqXHR, textStatus, errorThrown) {

      console.log(textStatus, errorThrown);

    }

  })


}

任何人都可以为我指出如何使用ajax以及如何配置php文件的正确方向?


文件 :


 //Check connection

if($link === false){

    die("ERROR: Could not connect. " . mysqli_connect_error());

}



$data = isset($_POST['arraykey']);

if ($data)

{

  $array = $_POST["arraykey"];

  echo $array;

  echo " is your array";

else 

{

  $array = null;

  echo "no array supplied";

}




/*$data = isset($_POST['OBJ']);


print_r($_POST);

*/

// close connection

mysqli_close($link);


?>

返回一个var_dump($_POST);array(0) { }

更新:ajax似乎成功了,但没有数组传递给php。


交互式爱情
浏览 64回答 1
1回答

冉冉说

由于有多个值,您可以使用多个值向其添加多个值,并将其发送到ajax。divsarray演示代码 :document.getElementById("done_editing").addEventListener("click", get_values);var OBJ = [];function get_values() {&nbsp; let divobj = document.querySelectorAll('[contenteditable=true]')&nbsp; for (var i = 0; i < divobj.length; i++) {&nbsp; &nbsp; OBJ.push(divobj[i].textContent); //add in array&nbsp;&nbsp; }&nbsp; console.log(OBJ)&nbsp; //your ajax call&nbsp; $.ajax({&nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; url: 'test.php',&nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; OBJ: OBJ //pass array to php&nbsp;&nbsp; &nbsp; },&nbsp; &nbsp; success: function(data) {&nbsp; &nbsp; &nbsp; alert("something");&nbsp; &nbsp; }&nbsp; });}div {&nbsp; border: 1px solid;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div contentEditable="true"></div><div contentEditable="true"></div><div contentEditable="true"></div><div contentEditable="true"></div><button id="done_editing">Done</button>你的 php 将如下所示:&nbsp; <?php&nbsp; &nbsp; // You should call this first&nbsp; &nbsp; &nbsp;session_start();&nbsp; &nbsp; $data = isset($_POST['arraykey']);&nbsp; &nbsp; &nbsp;if ($data)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;$_SESSION['arraykey'] = $_POST["arraykey"];//putting aray value in session&nbsp; &nbsp; &nbsp; &nbsp;echo&nbsp; $_SESSION['arraykey'];&nbsp; &nbsp; &nbsp; &nbsp;echo " is your array";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; echo "no array supplied";&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; ?>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript