在我的PHP代码中,我只想连接到MySql数据库,然后检查长度值,但是当我这样做时,它会将页面重定向到PHP文件。如何仅通过主页面显示警报?
<?php
$servername = "localhost";
$username = "root";
$password = "abc123";
$dbname = "redstonegaming";
$ip = get_client_ip();
$datetime = date("l jS \of F Y h:i:s A");
$temp1 = $_GET["name"];
$temp2 = $_GET["comment"];
$comment = "";
$commentname = "";
if (strlen($temp1) < 1)
{
echo "<script>alert('Your message must be more than 0 characters!');</script>";
}
else if (strlen($temp2) > 300)
{
echo "<script>alert('Your message must be less than 300 characters!');</script>";
}
else {
send();
}
function get_client_ip() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
echo "<script>alert('Connection failed: ' . $conn->connect_error);</script>";
}
function send()
{
$sql = "INSERT INTO comments(Name, Message, Ip_Address, dateTime) VALUES ('$commentname','$comment','$ip','$datetime')";
}
if ($conn->query($sql) === TRUE)
{
echo "<script>alert('New Record Created Successfully!');</script>";
}
else
{
echo "<script>alert('Error: ' . $sql . '<br>' . $conn->error');</script>";
}
$conn->close();
?>
var rick = false;
var audio = new Audio('rick_roll.mp3');
var kkeys = [],
konami = "38,38,40,40,37,39,37,39,66,65,13";
$(document).keydown(function(e) {
kkeys.push(e.keyCode);
if (kkeys.toString().indexOf(konami) >= 0) {
kkeys = []; // <-- Change here
if (rick == false) {
rick = true;
audio.play();
} else if (rick == true) {
rick = false;
audio.pause(); // <-- another issue
}
}
});
&#13;
/*Some Fonts Here:*/
@font-face { font-family: Rusty; src: url('BrushScriptStd.otf');}
* {
font-family: Rusty;
font-weight: Lighter;
}
.background
{
background-image: url(0.jpg);
background-attachment: fixed;
background-size: 100% auto;
background-color: f7f7f7;
background-repeat: no-repeat;
background-position:absolute;
}
.menubar {
position: relative;
height: 2.8vw;
opacity: 0.85;
background-color: #CCCCCC;
z-index: 1;
}
.clearfix:after {
display: block;
clear: both;
}
.menu-wrap {
width: 50%;
box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.2);
background: #3e3436;
}
.menu {
width: 100%;
margin: 0px auto;
text-align: center;
}
.menu li {
margin: 0px;
list-style: none;
font-family: 'Ek Mukta';
}
.menu a {
transition: all linear 0.15s;
color: #919191;
}
.menu li:hover > a,
.menu .current-item > a {
text-decoration: none;
color: rgba(189, 34, 34, 1);
}
.menu .arrow {
font-size: 0.95vw;
line-height: 0%;
}
.menu > ul > li {
float: middle;
display: inline-block;
position: relative;
font-size: 1.2vw;
}
.menu > ul > li > a {
padding: 0.7vw 5vh;
display: inline-block;
text-shadow: 0px 1px 0px rgba(0, 0, 0, 0.4);
}
.menu > ul > li:hover > a,
.menu > ul > .current-item > a {
background: #2e2728;
}
.menu li:hover .sub-menu {
display: block;
}
.sub-menu {
width: 100%;
padding: 0px 0px;
position: absolute;
top: 100%;
left: 0px;
display: none;
transition: opacity linear 5.8s;
box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.2);
background: #2e2728;
}
.sub-menu li {
display: block;
font-size: 1.2vw;
}
.sub-menu li a {
padding: 10px 10px;
display: block;
}
.sub-menu li a:hover, .sub-menu .current-item a {
background: #3e3436;
}
.Rusty
{
font-family: "Rusty";
color: rgba(189, 34, 34, 1);
display: inline;
}
.content
{
opacity: .85;
position: relative;
margin: auto;
width: 80%;
z-index: 0;
background-color: #CCCCCC;
padding: 10px;
height: 100%;
text-align: center;
}
.menu > ul > .login
{
position: absolute;
top: 0;
right: 0;
}
.video-container {
position:relative;
padding-bottom: 5%;
padding-top: 30px;
width: 100%;
}
.video-container iframe, .video-container object, .video-container embed {
position: relative;
top:15%;
width: 27vw;
height:27vh;
}
.title
{
text-align: center;
font-size: 7vh;
text-decoration: underline;
-moz-text-decoration-color: inherit;
text-decoration-color: inherit;
}
.feed-column
{
width: 50%;
height: 100%;
}
.border-right
{
border-right: thick solid rgba(189, 34, 34, 1);
}
.text-center
{
text-align: center;
}
.footer
{
background: #CCCCCC;
position:fixed;
bottom:0px;
height: 50px;
width: 100%;
opacity: .85;
}
.yt-title
{
font-size: 3.3vh;
}
&#13;
<html>
<head>
<meta name="generator"
content="HTML Tidy for HTML5 (experimental) for Windows https://github.com/w3c/tidy-html5/tree/c63cc39" />
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://s.ytimg.com/yts/cssbin/www-subscribe-widget-webp-vflj9zwo0.css"
name="www-subscribe-widget" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="script.js"></script>
<link rel="shortcut icon" href="favicon.ico" />
<title>RG - Home</title>
<style>
#comment_form input, #comment_form textarea {
border: 4px solid rgba(0,0,0,0.1);
padding: 8px 10px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
outline: 0;
}
#comment_form textarea {
width: 350px;
}
#comment_form input[type="submit"] {
cursor: pointer;
background: -webkit-linear-gradient(top, #efefef, #ddd);
background: -moz-linear-gradient(top, #efefef, #ddd);
background: -ms-linear-gradient(top, #efefef, #ddd);
background: -o-linear-gradient(top, #efefef, #ddd);
background: linear-gradient(top, #efefef, #ddd);
color: #333;
text-shadow: 0px 1px 1px rgba(255,255,255,1);
border: 1px solid #ccc;
}
#comment_form input[type="submit"]:hover {
background: -webkit-linear-gradient(top, #eee, #ccc);
background: -moz-linear-gradient(top, #eee, #ccc);
background: -ms-linear-gradient(top, #eee, #ccc);
background: -o-linear-gradient(top, #eee, #ccc);
background: linear-gradient(top, #eee, #ccc);
border: 1px solid #bbb;
}
#comment_form input[type="submit"]:active {
background: -webkit-linear-gradient(top, #ddd, #aaa);
background: -moz-linear-gradient(top, #ddd, #aaa);
background: -ms-linear-gradient(top, #ddd, #aaa);
background: -o-linear-gradient(top, #ddd, #aaa);
background: linear-gradient(top, #ddd, #aaa);
border: 1px solid #999;
}
.align-middle
{
position: absolute;
}
</style>
</head>
<body class="background">
<div class="menubar">
<nav class="menu">
<ul class="clearfix">
<li>
<a href="aboutme.html">About Me
<span class="arrow">▼</span></a>
<ul class="sub-menu">
<li>
<a href="#">Gaming</a>
</li>
<li>
<a href="#">Programming</a>
</li>
<li>
<a href="#">YouTube</a>
</li>
<li>
<a href="#">Other</a>
</li>
</ul>
</li>
<li>
<a href="schedule.html">Schedule</a>
</li>
<li class="current-item">
<a href="#">
<img style="width: 2.3vh; height: 1.6vw;" align="left" src="favicon.ico" />
<p align="middle" class="rusty">RedstoneGaming</p>
<img style="width: 2.3vh; height: 1.6vw;" align="right" src="favicon.ico" />
</a>
</li>
<li>
<a href="equipment.html">Equipment</a>
</li>
<li>
<a href="contact.html">Contact Me</a>
</li>
<li class="login">
<a href="login.html">Login/Sign Up</a>
</li>
</ul>
</nav>
</div>
<div class="content">
<h1 class="rusty title">RedstoneGaming Comments</h1>
<div class="border-right feed-column align-middle">
<div id="comment_form">
<form action="welcome.php" method="get">
<input type="text" name="name" id="name" value="" placeholder="Name" />
<textarea rows="10" name="comment" id="comment" placeholder="Enter your message here"></textarea>
<input type="submit" name="submit" value="Add Comment" /></form>
</div>
</div>
<div class="feed-column"></div>
</div>
<div class="footer">vv</div>
</body>
</html>
&#13;
答案 0 :(得分:1)
您在表单中未包含 action =&#34;&#34; 标记。 所以当你按提交时,脚本不知道在哪里执行。
通过
修复它<form action="welcome.php" method="get" action="">
对于您的问题,最简单的解决方案是将PHP脚本包含在HTML文档的顶部,其中包含空操作=&#34;&#34; 标记。它将在该页面上查找php脚本。
注意这是一种不好的做法。如果您在操作标记中使用其他页面,请确保在PHP中添加header()函数以重定向(如果需要,可以在某处)。
还有一个通知。 你的函数send()不会执行SQL,它基本上只是将sql语句存储在变量中。
希望它有所帮助,