I'm not a dev, I never do it but for a specific thing I've made a web page where people can insert data into a sql database.
My code is working but I guess I can optimize it because I've made it from things I've found over the Internet and I would like to turn radio buttons into check boxes because it is a lot of buttons at the end when there are many lines. Maybe there are things wrong because I have change names so there are no ref to my company.
<!DOCTYPE html>
<?php
include('Connexion_DB.php');
/* Queries */
if (isset($_POST['Btn_Mode1'])){
$req1 = "UPDATE DATABASENAME SET Mode1 = '".$_POST['Mode1']."' WHERE ID = '".$_POST['ID']."'";
$result1 = sqlsrv_query($con,$req1);
}
if (isset($_POST['Btn_Mode2'])){
$req2 = "UPDATE DATABASENAME SET Mode2 = '".$_POST['Mode2']."' WHERE ID = '".$_POST['ID']."'";
$result2 = sqlsrv_query($con,$req2);
}
if (isset($_POST['form1']))
{
$Status = 'Y';
$array = explode(PHP_EOL, $_POST['textareaname']);
foreach($array as $index=>$value) {
if($value === '') unset($array[$index]);
}
foreach ($array as $key => $value) {
$req = "INSERT INTO DATABASENAME (DATA, Active, Mode1, Mode2) VALUES ('".$value."','".$Status."', 'false','false');";
$result = sqlsrv_query($con,$req);
}
echo "<pre>";
echo print_r($array);
echo "</pre>";
}
if (isset($_POST['form2'])) {
foreach ($_POST['ID'] as $id) {
$req3 = "DELETE FROM DATABASENAME WHERE ID = '".$id."'";
$result3 = sqlsrv_query($con,$req3);
}
}
include('menu.php');
?>
<html>
<head>
<title>TITLE</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" >
</head>
<script>
$(function() {
$('.RadioBtn_Mode1').change(function() {
Mode1 = $(this).val();
ID = $(this).attr('id');
$.post( "Test_Page.php", {
Btn_Mode1: true,
ID: $(this).attr('id'),
Mode1: $(this).val()
} ).done(function( ) {
toastr.success(ID +' est a ' +Mode1);
});
});
});
$(function() {
$('.RadioBtn_Mode2').change(function() {
Mode2 = $(this).val();
ID = $(this).attr('id');
$.post( "Test_Page.php", {
Btn_Mode2: true,
ID: $(this).attr('id'),
Mode2: $(this).val()
} ).done(function( ) {
toastr.success(ID + ' est a ' +Mode2);
});
});
});
</script>
<body>
<div class="container body-container">
<div id='titre'class='titre'><h1>Insert Data</h1></div>
<form method='post' >
<input type="hidden" name="form1" />
<br />
<p>Data :</p>
<textarea name="textareaname" cols="60" rows="5"></textarea>
<br />
<br />
<input type="submit" value="Submit"/>
</form>
<form method="post" action="Test_Page.php">
<input type="hidden" name="form2">
<input type="submit" id="deleteBtn" value="Delete"/>
<div class="nav" id="inst1">
<table class="one">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Active</th>
<th scope="col">Data</th>
<th colspan="2">Mode1</th>
<th colspan="2">Mode2</th>
</tr>
</thead>
<tbody>
<?php
$req1 = "SELECT ID, Active, Data, Mode1, Mode2 FROM DATABASENAME" ;
$result1 = sqlsrv_query($con,$req1);
while($row1 = sqlsrv_fetch_array($result1))
{
echo "<tr>";
echo "<td><input type='checkbox' name = 'ID[]' value='" . $row1['ID'] . "'/></td>";
echo '<td>'.$row1['Active'].' </td>
<td>'.$row1['Data'].' </td>';
if ($row1['Mode1']=='False'){
echo '<td>False : <input class="RadioBtn_Mode1" id="'.$row1['ID'].'" name="RadioBtn_Mode1'.$row1['ID'].'" type="radio" value="False" checked/></td>';
echo '<td>True : <input class="RadioBtn_Mode1" id="'.$row1['ID'].'" name="RadioBtn_Mode1'.$row1['ID'].'" type="radio" value="True"/></td>';
} else {
echo '<td>False : <input class="RadioBtn_Mode1" id="'.$row1['ID'].'" name="RadioBtn_Mode1'.$row1['ID'].'" type="radio" value="False"/></td>';
echo '<td>True : <input class="RadioBtn_Mode1" id="'.$row1['ID'].'" name="RadioBtn_Mode1'.$row1['ID'].'" type="radio" value="True" checked/></td>';
}
if ($row1['Mode2']=='False'){
echo '<td>False : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="False" checked/></td>';
echo '<td>True : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="True"/></td>';
} else {
echo '<td>False : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="False" /></td>';
echo '<td>True : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="True" checked /></td>';
}
echo
'</td>
</tr>';
}
?>
</tbody>
</table>
</div>
</form>
</body>
</html>
<?php
sqlsrv_close($con);
?>
I would like a master to tell me what I can do better and help me to turn radio buttons into check boxes for the data insert. SQL format table is BIT, so I want to insert true / false when I check/uncheck like I do with buttons. Thanks !
Aucun commentaire:
Enregistrer un commentaire