I would like to check from my PHP file, if checkboxes are selected on my Main.html page, when I click a button on this page. If so do some work in the PHP file and return results (JSON) on the HTML Page
I have the following code so far:
Below is the HTML code with the tickboxes and a script using AJAX to receive the response on the Main.html webpage (and as I will be doing some work within the PHP file on the server):
<form action="available_evidence">
<div id ="checkbox1"><input type="checkbox" name="Fb_check" id="Fb_check" value="Bike"> Facebook<br></div>
<div id ="checkbox2"><input type="checkbox" name="Mess_check" id="Mess_check" >Facebook Messenger<br></div>
<div id ="errorMessage">
<p>Error: No Evidence Avaialble.<br>Please Check Evidence Directory</p>
</div>
</form>
<form id="display" method="post" action="PHP_Function_2.php">
<input type="submit" class="learnButton" name="insert" value="Load Location Evidence" />
</form>
<script>
$(function(){
$("#display").submit(function(event) {
event.preventDefault();
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: $(this).serialize(),
dataType: 'json',
success: function(data)
{
//display data...
$("#results").empty();
$("#results").append(data);
console.log(data);
}
});
});
});
Below is the PHP_Function_2.php file in which I wish to check if the tickboxes are selected, if so do something, in this case just post that if either box is:
<?php
$test = "Fb Checked !";
$test2 = "Fb messenger checked";
//IF TICKBOX IS SELECTED
if (isset($_POST['Fb_check'])) {
echo json_encode($test);
// Checkbox is selected
} elseif(isset($_POST['Mess_check'])){
echo json_encode($test2);
}
elseif((isset($_POST['Mess_check'])) && (isset($_POST['Fb_check'])))
{
echo json_encode($test);
echo json_encode($test2);
}
?>
I would really appreciate some help getting this to work, as I am new to PHP and have been stuck for a while. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire