2016-02-22 56 views
0
function create(x) { 
       var field=document.createElement('fieldset'); 
          var t=document.createElement('table'); 
          t.setAttribute("id","myTable"); 
          document.body.appendChild(t); 
          field.appendChild(t); 
          document.body.appendChild(field); 

          var row=document.createElement('th'); 
          newHeader = document.createElement("th"); 
          newHeader.innerText = x; 
          row.appendChild(newHeader); 


          var row1=document.createElement('tr'); 
          var col1=document.createElement('td'); 
          var col2=document.createElement('td'); 

          var row2=document.createElement('tr'); 
          var col3=document.createElement('td'); 
          var col4=document.createElement('td'); 

          var row3=document.createElement('tr'); 
          var col5=document.createElement('td'); 
          var col6=document.createElement('td'); 

          col1.innerHTML="Name"; 
          col2.innerHTML="<input type='text' name='stateactivityname' size='40' required>"; 
          row1.appendChild(col1); 
          row1.appendChild(col2); 
          col3.innerHTML="Registration Applicable"; 
          col4.innerHTML="<select name='regapp' required><option></option><option>Yes</option><option>No</option></select>"; 
          row2.appendChild(col3); 
          row2.appendChild(col4); 
          col5.innerHTML="Registers Applicable"; 
          col6.innerHTML="<select name='registers' required><option></option><option>Yes</option><option>No</option></select>"; 
          row3.appendChild(col5); 
          row3.appendChild(col6); 
          t.appendChild(row); 
          t.appendChild(row1); 
          t.appendChild(row2); 
          t.appendChild(row3); 
          addrow('myTable'); 
     } 

PHP代碼,用於存儲數據的數據庫動態創建的是:如何保存數據是在表內使用JavaScript數據庫

<html> 
<head> 
    <meta charset="UTF-8"> 
    <title></title> 
</head> 
<body> 
    <?php 
    $conn=new mysqli("localhost","root","","newcomplyindia"); 
    if($conn->connect_errno){ 
     echo("connection error"); 
    } 
    $actname=$_POST["actname"]; 
    $industry=$_POST['industrytype']; 
    $centralorstate=$_POST["cors"]; 
    $sql="insert into acts (actname,centralorstate) value ('".$actname."','".$centralorstate."')"; 
    $regapp=$_POST["regapp"]; 
    if($regapp=='Yes'){ 
     $regapp=true; 
    } 
    else{ 
     $regapp=false; 
    } 
    $registers=$_POST["registers"]; 
    if($registers=='Yes'){ 
     $registers=true; 
    } 
    else{ 
     $registers=false; 
    } 
    $sub=$_POST["sub"]; 
    if($sub=='Yes'){ 
     $sub=true; 
    } 
    else{ 
     $sub=false; 
    } 
    if($conn->query($sql)==true){ 
      echo 'act name added '; 
    } 
    $lastid=$conn->insert_id; 
    $sql1="insert into actsstate (actid,registrationrequired,registersapplicable,sublocation)" 
      . "values('$lastid','$regapp','$registers','$sub')"; 

    if($conn->query($sql1)==true){ 
     echo '<br>name and central/state added'; 
    } 
    $stateactivity=$_POST["stateactivityname"]; 
    $activityname=$_POST["activityname"]; 
    $activitymonth=$_POST["month"]; 
    $activitydate=$_POST["date"]; 
    $sql2="insert into activity (name,actid,activityname,activitymonth,activitydate)" 
     . "values('$stateactivity','$lastid','$activityname','$activitymonth','$activitydate')"; 
    if($conn->query($sql2)){ 
     echo 'activity added'; 
    } 
    else{ 
     echo 'no record'; 
    } 
    $conn->close(); 
    ?> 

我有這樣一個JavaScript。該表是動態創建的。我想將這個表內的數據存儲到數據庫中。我正在使用mysqli進行數據庫連接 上次訪問javascript。誰能幫我做這個

+0

你想用JS或PHP存儲它嗎?你爲PHP添加了一個標籤,那麼你使用的是服務器端? –

+0

是的..我想用php存儲它 – sree

+0

你有沒有寫任何PHP代碼?我們可能需要更多來給出足夠的答案 –

回答

0

當然可以通過使用AJAX:

$.post("php_script.php",{javascript variables}, function(result) { 
    alert(result); 
}); 
0

下面是使用香草JS(純JS)

var xhttp = new XMLHttpRequest(); 
var url = "save.php"; 
xhttp.open("POST", url, true); 
// uncomment this if you're sending JSON 
// xhttp.setRequestHeader("Content-type", "application/json"); 

xhttp.onreadystatechange = function() { // Call a function when the state changes. 
    if(xhttp.readyState == 4 && xhttp.status == 200) { 
     // the 4 & 200 are the responses that you will get when the call is successful 
     alert(xhttp.responseText); 
    } 
} 
xhttp.send('the data you want to send'); 

的一種方法,這裏有一個方法來保存到數據庫(在我的情況下,MySQL)與平的PHP(純PHP)

$servername = "localhost"; 
$username = "db_username"; 
$password = "db_password"; 
$dbname  = "db_name"; 

// connect to the DB 
$conn = new mysqli($servername, $username, $password, $dbname); 
// check if you're connected 
if ($conn->connect_error) { 
    echo "Connection failed: " . $conn->connect_error; 
} 
else { 
    // echo "connecting to DB succeeded <br> "; 
} 

// uncomment the following if you're recieving json 
// header("Content-Type: application/json"); 
// $array = json_decode(file_get_contents("php://input"), true); 

$sql = "INSERT INTO table_name (columns,names) VALUES (columns,values)"; 
if ($conn->query($sql) === TRUE) { 
    echo "Data was saved successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 

$conn->close(); 

瞭解更多關於sql命令我建議w3schools tutorials

+0

我需要php代碼來保存數據到數據庫 – sree

+0

@sree我加了一些 –

+0

@sree你得到的錯誤是什麼? –

相關問題