附件是我的程序:
HTML:
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading">Contact Us</h2>
<h3 class="section-subheading text-muted">Lorem ipsum dolor sit amet consectetur.</h3>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<form name="sentMessage" id="contactForm" action="converts.php" method="post" enctype="multipart/form-data" autocomplete="off">
<div class="alert alert-error"><?= $_SESSION['message'] ?>
<style>
.alert {
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 4px 20px 4px 20px;
font-size: 13px;
line-height: 20px;
margin-bottom: 20px;
text-shadow: none;
position: relative;
background-color: #272e3b;
color: rgba(255, 255, 255, 0.7);
border: 1px solid #000;
box-shadow: 0 0 0 1px #363d49 inset, 0 5px 10px rgba(0, 0, 0, 0.75);
}
.alert-error {
color: #f00;
background-color: #360e10;
box-shadow: 0 0 0 1px #551e21 inset, 0 5px 10px rgba(0, 0, 0, 0.75);
}
.alert:empty{
display: none;
}
.alert-success {
color: #21ec0c;
background-color: #15360e;
box-shadow: 0 0 0 1px #2a551e inset, 0 5px 10px rgba(0, 0, 0, 0.75);
}
</style>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" placeholder="Your Address *" id="address" required data-validation-required-message="Please enter address here."></textarea>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="cell district *" id="district" required data-validation-required-message="Enter district here.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<input type="submit" value="Send Message" name="register" class="btn btn-xl"/>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
PHP:
<?php
session_start();
$_SESSION['message'] = '';
$mysqli = new mysqli('localhost', 'root', 'root', 'accounts');
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['address']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$address = strip_tags(htmlspecialchars($_POST['address']));
// Create the email and send the message
$to = 'laolufasanya@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nAddress:\n$address";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
JavaScript的:
$(function() {
$("#contactForm input,#contactForm textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var district = $("input#district").val();
var address = $("textarea#address").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
console.log(window);
$.ajax({
url: window.origin + "/converts.php",
type: "POST",
data: {
name: name,
phone: phone,
email: email,
address: address,
district: district
},
cache: false,
success: function(res) {
console.log(res);
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
});
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
$('#success').html('');
});
所有查询都没有更新。我一直试图完成这项工作,没有任何工作。我已经尝试了书中的所有内容。
实际PHP代码:
PHP:
<?php
session_start();
$_SESSION['message'] = '';
//connection to database
$mysqli = new mysqli('localhost', 'root', 'root', 'accounts');
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
echo "Connected successfully";
// sql to create table
//$sql = "CREATE TABLE Added (
//id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
//name VARCHAR(30) NOT NULL,
//email VARCHAR(30) NOT NULL,
//address VARCHAR(30) NOT NULL,
//phone VARCHAR(30) NOT NULL,
//district VARCHAR(50),
//reg_date TIMESTAMP
//)";
//if ($mysqli->query($sql) === TRUE) {
//echo "Table Added created successfully";
//} else {
//echo "Error creating table: " . $mysqli->error;
//}
//update database
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//two passwords are equal to each other
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
$phone = $mysqli->real_escape_string($_POST['phone']);
$address = $mysqli->real_escape_string($_POST['address']);
$district = $mysqli->real_escape_string($_POST['district']);
//make sure file type is image
$sql = "INSERT INTO Added (name, email, phone, address, district) "
. "VALUES ('$name', '$email', '$phone', '$address', '$district')";
var_dump($sql);
die();
if ($mysqli->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $mysqli->error;
}
}
Help would be highly regarded on this issue, Thank you.
I have tried everything and it just keeps taking my time.
在这个问题上,帮助会受到高度重视,谢谢。
I have tried everything and it just keeps taking my time.
我相信我已经遵循了如何完成这项工作的所有规则,但是 我一直面对着 绊脚石,希望这一切都可以由你的 帮助
我之前插入了一个php代码,但是没有删除它,这是在我的数据库上创建了一个表的实际代码。它有点儿 显示每个内容都在更新,但值仍为空。
答案 0 :(得分:0)
您在查询之前正在调用die()。之后什么都不会被执行。尝试删除该行。