我已经在bootstrap 4模态中插入了一个表单,并且试图将用户信息发布到myphpadmin数据库中,并使用ajax函数自动更新页面,以便用户可以使用新信息。以下是我用来尝试实现此目标的所有代码,模态,ajax和insert.php 当我单击模态上的插入按钮时,模态正确消失,但信息不会进入数据库并且不会更新网站上的表。 我非常了解php / mysql / ajax,并一直呆在整个下午,并且非常感谢您对我做错的任何帮助/指导。
<!-- Modal -->
<div class="modal fade" id="addmedicationmodal" tabindex="-1" role="dialog" aria-labelledby="medicationmodal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add Medication</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form method="POST" id="insert_form">
<div class="modal-body">
<div class='form-group'>
<label>Medication Name</label>
<select name='uploadmed' class="form-control">
<option selected>Choose medication...</option>
<?php
$medicationdropdown = "SELECT * FROM medi_medication";
$dropresult = $conn -> query($medicationdropdown);
if(!$dropresult){
echo $conn->error;
}
while ($drop = $dropresult->fetch_assoc()){
$medname = $drop['name'];
$medid = $drop['id'];
echo"<option value ='$medid'>$medname</option>";
}
?>
</select>
</div>
<div class='form-group'>
<label>Dose</label>
<input type="text" class= "form-control" name='uploaddose' id="dosage" placeholder="Dosage" required>
</div>
<div class='form-group'>
<label>Medical Condition</label>
<input type="text" class= "form-control" name='uploadcondition' id="medical_condition" placeholder="Medical Condition" required>
</div>
<div class='form-group'>
<label>Instructions</label>
<textarea class= "form-control" name='uploadinstructions' id="instructions" placeholder="Instructions" required></textarea>
</div>
<div class='form-group'>
<label>Benefits</label>
<textarea class= "form-control" name='uploadbenefits' id="benefits" placeholder="Benefits" required></textarea>
</div>
<div class='form-group'>
<label>Side Effects</label>
<textarea class= "form-control" name='uploadsideeffects' id="side_effects" placeholder="Side Effects" required></textarea>
</div>
<div class='form-group'>
<label>Times Per Day</label>
<input type= text class= "form-control" name='uploadtimesperday' id="times_per_day" placeholder="Times Per Day" required></textarea>
</div>
<input type ="hidden" id="patientid" name="postid" value = "$patient">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" name = 'insertdata' class="btn btn-primary">Add Medication</button>
</div>
</form>
</div>
</div>
</div>
Ajax脚本:
<script>
$('#insert_fomr').on('submit', function(event){
$.ajax({
url: "insert.php",
method: "POST",
data:$('insert_form').serilize(),
success:function(data)
{
$('#insert_form')[0].reset();
$('#addmedicationmodal').modal('hide');
$('#medication_table').html(data);
}
});
});
</script>
Insert.php代码:
$connect = mysql_connect("$host","$user","$pw","$db")
if(!empty($_POST))
{
$output ='';
$patientid =mysqli_real_escape_string($connect, $_POST['postid']);
$medtype =mysqli_real_escape_string($connect, $_POST['uploadmed']);
$dosage =mysqli_real_escape_string($connect, $_POST['uploaddose']);
$medical_condition =mysqli_real_escape_string($connect, $_POST['uploadcondition']);
$instructions = =mysqli_real_escape_string($connect, $_POST['uploadinstructions']);
$benefits =mysqli_real_escape_string($connect, $_POST['uploadbenefits']);
$sideeffects =mysqli_real_escape_string($connect, $_POST['uploadsideeffects']);
$timesperday =mysqli_real_escape_string($connect, $_POST['uploadtimesperday'])
$query ="INSERT INTO medi_user_medication(user_id,medication_id,dosage,medical_condition,instructions,benefits,side_effects,times_per_day)
VALUES ('$patientid', '$medtype', '$dosage','$medical_condition', '$instructions', '$benefits','$sideeffects', '$timesperday')";
if(mysqli_query($connect, $query))
{
$output .= '<label class="text-success"> Medication Added</label>';
$select_query = "SELECT medi_user_medication.user_id, medi_medication.name, medi_user_medication.dosage,
medi_user_medication.medical_condition, medi_user_medication.instructions, medi_user_medication.benefits,
medi_user_medication.side_effects, medi_user_medication.times_per_day
FROM
medi_user_medication
INNER JOIN
medi_medication
ON
medi_user_medication.medication_id = medi_medication.id
WHERE
medi_user_medication.user_id ='$patientid'";
$result = mysqli_query($connect, $select_query);
$output .= '
<table class="table table-striped table-bordered table-hover patientsTable" style="width: 100%">
<thead>
<tr>
<th scope="col">Medication</th>
<th scope="col">Dosage</th>
<th scope="col">Medical Condition</th>
<th scope="col">Instructions</th>
<th scope="col">Benefits</th>
<th scope="col">Side Effects</th>
<th scope="col">Times Per Day</th>
</tr>
</thead>';
while($row = mysqli_fetcg_array($result))
{
$.output .= '
<tr>
<td> . row["uploadmed"] . </td>
<td> . row["uploaddose"] .</td>
<td> . row["uploadcondition"] .</td>
<td> . row["uploadinstructions"] .</td>
<td> . row["uploadbenefits"] .</td>
<td> . row["uploadsideeffects"] .</td>
<td> . row["uploadtimesperday"] .</td>
</tr>
';
}
$output .= '</table>'
}
echo $output;
}
?>