2016-07-16 52 views
0

我有一个非常简单的网络应用程序,它允许用户输入名和姓,并点击提交以将名称插入到sql数据库中。 在表单下方有一个HTML表格,显示数据库表格的内容。如何使用PHP与JavaScript变量进行交互?

如果用户单击数据库的任意一行,则使用以下函数将HTML元素的ID(设置为与数据库ID相同的值)保存到JavaScript变量中。

<!--JS Functions--> 
<script> 
//Determine which record is selected. 
var id; 
function TableSelect(el) { 
id = el.id; 
alert(id); 
} 
</script> 

这里是以下形式:

<!--HTML Form --> 
<form method="post" action="index.php"> 
    First Name: <input type="text" name="FirstName" value=""> 
    <br><br> 
    Last Name: <input type="text" name="LastName" value="<?php echo $LastName;?>"> 
    <br><br> 
    <input type="submit" name="submit" value="Submit"> 
    <input type="submit" name="delete" value="Delete" onclick="Delete()"> 
</form> 

这里是数据的PHP处理和SQL表的输出:

//prepare query to insert data from form into DB 
if (isset($_POST['submit'])){ 
    $query = "INSERT INTO tbl_HR_Master (First_Name, Last_Name) VALUES ('{$FirstName}', '{$LastName}') "; 
    $result = mysqli_query($connection, $query); 
    //Test if there was a query error 
    querycheck($result); 
}//end if 

//prepare query to populate table from DB 
$query2 = "Select id, First_Name as 'First Name', Last_Name as 'Last Name' from tbl_HR_Master"; 
$result2 = mysqli_query($connection, $query2); 
//Test if there was a query error 
querycheck($result2); 

//display table 
echo "</br>"; 
echo "<table id=\"tbl_HR_Master\" border='1'><tr class=\"nohover\"\">"; 
// printing table headers 
$fields_num = mysqli_num_fields($result2); 
for($i=0; $i<$fields_num; $i++) { 
    $field = mysqli_fetch_field($result2); 
    echo "<td>{$field->name}</td>"; 
} //end for 
echo "</tr>\n"; 
// print table rows 
while($row = mysqli_fetch_row($result2)) 
{ 
    echo "<tr>"; 
    $id = $row[0]; 
    foreach($row as $cell){ 
    echo "<td onclick=TableSelect(this) id=". $id .">".$cell."</td>"; 
    }//end foreach 
    echo "</tr>\n"; 
}//end while 

我想要运行一个PHP函数,其从数据库中删除选定的记录,然而,显然PHP运行在服务器上,因此,我需要一些方法告诉PHP哪些记录被选中。再一次,如果你看看我的Javascript函数,var id =最后选择的记录。我如何解析这个JS变量到服务器进行处理?

概括地说,要做到这一点在PHP中:

//delete selected record 
if (isset($_POST['delete'])){ 
    $query3 = "Delete from tbl_HR_Master where id = ". JS VARIABLE HERE." "; 



} //end if 

回答

1

基于表单的

你可以与你给一个特定ID的隐藏表单域做到这一点:

<input type="hidden" id="your-id" value="" /> 

然后在您的TableSelect函数中指定值:

document.getElementById('your-id').value = id; 

然后你就可以访问变量像其他请求(POST/GET)参数,你的情况:基于Ajax的

$_POST['id'] 

使用jQuery你可以执行一个AJAX请像这样在你的TableSelect功能:

$.ajax({ 
     url: 'file.php', 
     type: 'post', 
     data: {'id': el.id}, 
     success: function(data, status) { 
     // ... 
     } 
}); 

请求参数访问相同:

$_POST['id'] 
+0

这给了我上次输入的记录,但是我需要最后一次点击。每次功能运行时,我是否需要“发布”? – mrgunston

+0

作为其他参数,如果您提交表单,它将只被转移到后端。 或者你可以执行ajax调用,你可以看看:https://api.jquery.com/jquery.ajax/ – ScientiaEtVeritas

0

你可以在你的窗体中有一个隐藏的变量,并使用javascript来设置该隐藏变量的值onclick。

<!--JS Functions--> 
<script> 
//Determine which record is selected. 
var id; 
function TableSelect(el) { 
    document.getElementById('selectedRow').value = el.id; 
} 
</script> 

,并添加您的表单标签内的下列

<input type="hidden" name="selectedRow" id="selectedRow"> 

然后你就可以更新你的查询是这样的

$query3 = "Delete from tbl_HR_Master where id = $_POST['selectedRow']"; 

当然不要忘了添加消毒和验证和所有爵士乐。