如何将默认字符串与表单一起发送?
How to send a default string together with the form?
<?php
require_once 'connection.php';
$slug = '';
if(isset($_POST["create"])){
$slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_POST["title"])));
$query = "SELECT slug_url FROM bn_publicacao WHERE slug_url LIKE '$slug%'";
$statement = $conn->prepare($query);
if($statement->execute()){
$total_row = $statement->rowCount();
if($total_row > 0){
$result = $statement->fetchAll();
foreach($result as $row){
$data[] = $row['slug_url'];
}
if(in_array($slug, $data)){
$count = 0;
while( in_array( ($slug . '-' . ++$count ), $data) );
$slug = $slug . '-' . $count;
}
}
}
$insert_data = array(
':title' => $_POST['title'],
':descricao' => $_POST['descricao'],
':capa' => $_POST['capa'],
':alt' => $_POST['alt'],
':keywords' => $_POST['keywords'],
':categoria' => $_POST['categoria'],
':slug_url' => $slug,
':slug_link' => $slug,
':entry_type' => $_POST['entry_type'],
);
$query = "INSERT INTO bn_publicacao (title, descricao, capa, alt, keywords, categoria, slug_url, slug_link, entry_type) VALUES (:title, :descricao, :capa, :alt, :keywords, :categoria, :slug_url, :slug_link, :entry_type)";
$statement = $conn->prepare($query);
$statement->execute($insert_data);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<style>
.box
{
max-width:600px;
width:100%;
margin: 0 auto;;
}
</style>
</head>
<body>
<div class="container box">
<br />
<h3 align="center">Gravar post</h3>
<br />
<form method="post">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control mb-3" required />
<label>Descicao</label>
<input type="text" name="descricao" class="form-control mb-3" required />
<label>Capa</label>
<input type="text" name="capa" class="form-control mb-3" required />
<label>Alt</label>
<input type="text" name="alt" class="form-control mb-3" required />
<label>Keywords</label>
<input type="text" name="keywords" class="form-control mb-3" required />
<label>Categoria</label>
<input type="text" name="categoria" class="form-control mb-3" required />
<label>Entry_type</label>
<input type="text" name="entry_type" class="form-control mb-3" required />
</div>
<br />
<div class="form-group">
<input type="submit" name="create" class="btn btn-info" value="Enviar" />
</div>
<br />
<h4>Generated Slug - <?php echo $slug; ?></h4>
</form>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</body>
</html>
input
"title" 自动生成一个 slug 将发送到列 slug_url
和 slug_link
,我需要在 slug_url
.
我想在生成到列 slug_url
的 slug 上添加默认值 url。
示例:
我想将此 url 添加为生成的 slug 的默认值“http://example.com/”
这是 slug-gererated-to-the-column-slug-url
这是它应该如何发送到我的专栏 slug_url
:
http://example.com/this-is-the-slug-gererated-to-the-column-slug-url
很简单,改一下这部分代码就可以了:
$insert_data = array(
':title' => $_POST['title'],
':descricao' => $_POST['descricao'],
':capa' => $_POST['capa'],
':alt' => $_POST['alt'],
':keywords' => $_POST['keywords'],
':categoria' => $_POST['categoria'],
':slug_url' => "https://example.com/$slug",
':slug_link' => $slug,
':entry_type' => $_POST['entry_type'],
);
注意我刚刚更改了设置 :slug_url 列的行
你可以添加这样的东西
':slug_url' => "https://example.com/".$slug,
<?php
require_once 'connection.php';
$slug = '';
if(isset($_POST["create"])){
$slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_POST["title"])));
$query = "SELECT slug_url FROM bn_publicacao WHERE slug_url LIKE '$slug%'";
$statement = $conn->prepare($query);
if($statement->execute()){
$total_row = $statement->rowCount();
if($total_row > 0){
$result = $statement->fetchAll();
foreach($result as $row){
$data[] = $row['slug_url'];
}
if(in_array($slug, $data)){
$count = 0;
while( in_array( ($slug . '-' . ++$count ), $data) );
$slug = $slug . '-' . $count;
}
}
}
$insert_data = array(
':title' => $_POST['title'],
':descricao' => $_POST['descricao'],
':capa' => $_POST['capa'],
':alt' => $_POST['alt'],
':keywords' => $_POST['keywords'],
':categoria' => $_POST['categoria'],
':slug_url' => $slug,
':slug_link' => $slug,
':entry_type' => $_POST['entry_type'],
);
$query = "INSERT INTO bn_publicacao (title, descricao, capa, alt, keywords, categoria, slug_url, slug_link, entry_type) VALUES (:title, :descricao, :capa, :alt, :keywords, :categoria, :slug_url, :slug_link, :entry_type)";
$statement = $conn->prepare($query);
$statement->execute($insert_data);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<style>
.box
{
max-width:600px;
width:100%;
margin: 0 auto;;
}
</style>
</head>
<body>
<div class="container box">
<br />
<h3 align="center">Gravar post</h3>
<br />
<form method="post">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control mb-3" required />
<label>Descicao</label>
<input type="text" name="descricao" class="form-control mb-3" required />
<label>Capa</label>
<input type="text" name="capa" class="form-control mb-3" required />
<label>Alt</label>
<input type="text" name="alt" class="form-control mb-3" required />
<label>Keywords</label>
<input type="text" name="keywords" class="form-control mb-3" required />
<label>Categoria</label>
<input type="text" name="categoria" class="form-control mb-3" required />
<label>Entry_type</label>
<input type="text" name="entry_type" class="form-control mb-3" required />
</div>
<br />
<div class="form-group">
<input type="submit" name="create" class="btn btn-info" value="Enviar" />
</div>
<br />
<h4>Generated Slug - <?php echo $slug; ?></h4>
</form>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</body>
</html>
input
"title" 自动生成一个 slug 将发送到列 slug_url
和 slug_link
,我需要在 slug_url
.
我想在生成到列 slug_url
的 slug 上添加默认值 url。
示例:
我想将此 url 添加为生成的 slug 的默认值“http://example.com/”
这是 slug-gererated-to-the-column-slug-url
这是它应该如何发送到我的专栏 slug_url
:
http://example.com/this-is-the-slug-gererated-to-the-column-slug-url
很简单,改一下这部分代码就可以了:
$insert_data = array(
':title' => $_POST['title'],
':descricao' => $_POST['descricao'],
':capa' => $_POST['capa'],
':alt' => $_POST['alt'],
':keywords' => $_POST['keywords'],
':categoria' => $_POST['categoria'],
':slug_url' => "https://example.com/$slug",
':slug_link' => $slug,
':entry_type' => $_POST['entry_type'],
);
注意我刚刚更改了设置 :slug_url 列的行
你可以添加这样的东西
':slug_url' => "https://example.com/".$slug,