我尝试在结果之前和之后添加else语句。我希望直到表单提交后才显示结果。我只希望在未提交表单时显示表单,而仅在您自己提交表单后才显示结果。 这就是我到目前为止所拥有的...
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
<style>
.Spud-Bud-clip {
font-size: 95%;
display: flex;
line-height: normal;
margin: auto;
margin-bottom: 5em;
width: 85%;
height: 105px;
padding: 12px 20px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 10px;
background-color: #f8f8f8;
font-size: 12px;
resize: none;
}
.Spud-Bud-button {
float: right;
overflow: hidden;
}
myCapitalizeFunction {
text-transform:capitalize;
}
.error {color: #FF0000;}
</style>
</head>
<body>
<div class="container-fluid">
<h2>Hyperlink Generator</h2>
<?php
// define variables and set to empty values
$nameErr = $websiteErr = "";
$name = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
if (empty($_POST["website"])) {
$websiteErr = "URL is required";
} else {
$website = test_input($_POST["website"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="form-group row">
<label for="label_name-text-input" class="col-2 col-form-label"><span class="error">* </span>Name</label>
<div class="col-10">
<input type="text" name="name" class="form-control" value="<?php echo $name;?>" />
<span class="error"><?php echo $nameErr;?></span>
</div>
</div>
<div class="form-group row">
<label for="label_name-url-input" class="col-2 col-form-label"><span class="error">* </span>URL:</label>
<div class="col-10">
<input class="form-control" type="text" class="form-control" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span>
</div>
</div>
<div class="form-group row">
<label for="label_name-text-sumbit" class="col-2 col-form-label"></label>
<div class="col-10">
<input class="btn btn-outline-primary btn-lg" type="submit" name="submit" value="Submit" >
<input class="btn btn-outline-primary btn-lg" type="reset" value="Reset">
</form>
</div>
</div>
<?php
echo "<h5>Now click the button to copy the code. Next, paste the code.</h5>";
echo '<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>PREVIEW:</strong> <a href="' . $website . '" target="_blank" rel="noopener">' . $name . '</a>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>';
echo '<div class="container-fluid"><div class="input-group add-on"><textarea style="overflow:hidden;" class="form-control clip Spud-Bud-clip" onclick="this.focus();this.select()"><a href="' . $website . '" target="_blank" rel="noopener">' . $name . '</a></textarea></div>';
?>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.7.1/clipboard.min.js"></script>
<script>
(function($) {
'use strict'
$(function() {
$('.clip').before('<button class="btn btn-outline-primary Spud-Bud-button" data-clipboard-target><i class="fa fa-copy"></i></button>')
$('[data-clipboard-target]')
.tooltip({
title: 'Copy to clipboard',
placement: 'right',
})
var clipboard = new Clipboard('[data-clipboard-target]', {
target: function(trigger) {
return trigger.nextElementSibling
}
})
clipboard.on('success', function(e) {
e.clearSelection()
$(e.trigger)
.attr('data-original-title', 'Copied!')
.tooltip('show')
.blur()
.attr('data-original-title', 'Copy to clipboard')
})
clipboard.on('error', function(e) {
var modifierKey = /Mac/i.test(navigator.userAgent) ? '\u2318' : 'Ctrl-'
var fallbackMsg = 'Press ' + modifierKey + 'C to copy'
$(e.trigger)
.attr('data-original-title', fallbackMsg)
.tooltip('show')
.blur()
.attr('data-original-title', 'Copy to clipboard')
})
})
}(jQuery))
</script>
</div>
</body>
</html>
我尝试在结果之前和之后添加else语句。我希望直到提交表单后才显示结果。
这就是我到目前为止所拥有的...
我尝试在正文后面添加以下内容
<?php
//If form not submitted, display form.
if (!isset($_POST['submit'])||(($_POST['name']) == "")){
?>
然后我添加了:
<?php
//If form submitted, process input.
} else {
//Retrieve show string from form submission.
就在
之后// define variables and set to empty values
最后添加:
<?php
} ?>
在/ body之前