For a project I have, I need to write all the code on one page and finding it very challenging. I need ajax, js and PHP but am not allowed to reload the page. I currently have everything working after 6 hours but I want to show a message below my submit button with the information a person has entered into the form. I have looked at ajax success and was thinking is their a way to do it via that or even better just have a div place holder with the id of #message that will change to the information in my POST.
This is my current ajax and js
function myFunction() {
var title = document.getElementById("title").value;
var firstname = document.getElementById("firstname").value;
var lastname = document.getElementById("lastname").value;
var address = document.getElementById("address").value;
var teleno = document.getElementById("teleno").value;
var email = document.getElementById("email").value;
var dataString = 'title1=' + title + '&firstname1=' + firstname + '&lastname1=' + lastname + '&address1=' + address + '&teleno1=' + teleno + '&email1=' + email;
if (title == '' || firstname == '' || lastname == '' || address == '' || teleno == '' || email == '') {
alert("Please Fill All Fields");
} else {
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false,
success: function(information) {
alert(html);
}
});
}
return false;
along with my php, I’m just using this code so I can make sure the data is actually going into my tables.
$title = mysqli_real_escape_string($conn, $_POST['title1']);
$firstname = mysqli_real_escape_string($conn, $_POST['firstname1']);
$lastname = mysqli_real_escape_string($conn, $_POST['lastname1']);
$address = mysqli_real_escape_string($conn, $_POST['address1']);
$teleno = mysqli_real_escape_string($conn, $_POST['teleno1']);
$email = mysqli_real_escape_string($conn, $_POST['email1']);
if (isset($_POST['title1'])) {
$sql = "INSERT INTO form (title, firstname, lastname, address, teleno, email) VALUES ('$title','$firstname','$lastname','$address','$teleno','$email')";
$count = mysqli_num_rows($result);
if (mysqli_query($conn, $sql)) {
} else {
echo "Error: " . $sql .
"<br>" . mysqli_error($conn);
}
}
$sql2 = "Select * From form";
$result = mysqli_query($conn, $sql2);
$contacts = array();
while ($row = mysqli_fetch_array($result)) {
$contacts[] = $row;
}
mysqli_close($conn);
foreach ($contacts as $contact) :
echo $contact["id"] . " ";
echo $contact["title"] . " ";
echo $contact["firstname"] . " ";
echo $contact["lastname"] . " ";
echo $contact["address"] . " ";
echo $contact["teleno"] . " ";
echo $contact["email"] . " ";
echo "<br>";
endforeach;
?>