<button type="submit">ツイートする</button>
ここの文字をイメージにすることも普通にできるらしい
<button type="submit">ツイートする<img src="img/player_button08_plus.png"></button>
できた。
ただUI上、明らかに投稿ボタンだなとわかる画像にする必要があるとのこと。
exec.php(投稿処理)もつくりましょう
--->exec.php
<?php
session_start();
//セッション情報がないかfalse
if(empty($_SESSION["login"])){
header("Location: login.php");
exit();
}
//tweetがからっぽ
if(empty($_POST["tweet"])){
header("Location: index.php");
exit();
}
require_once("config.php");
$sql="INSERT INTO tweets(u_id,tweet) VALUES(:u_id,:tweet)";//プレースホルダを使う
$stmt=$pdo->prepare($sql);
$stmt->bindValue(":u_id",$_SESSION["u_id"],PDO::PARAM_STR);
$stmt->bindValue(":tweet",$_POST["tweet"],PDO::PARAM_STR);
$stmt->execute();
header("Location: index.php");
exit();
?>
どこでひっかかったかわかるようにこうする手がある
exit("1");
としておいたらこの部分にきたなと分かる。
さて、ツイート入力窓は「ログインしているとき」だけに表示させたい
if文で囲いましょう
ここまで
--->index.php
<?php
session_start();
require_once("config.php");
$sql="SELECT nickname,tweet,created FROM tweets,users WHERE tweets.u_id=users.u_id ORDER BY t_id DESC";
$res=$pdo->query($sql);
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Twitter タイムライン</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="container">
<h1>My Twitter</h1>
<!--からっぽでないばあい-->
<?php if(!empty($_SESSION["login"])): ?>
<div id="tweetform">
<form method="post" action="exec.php">
<textarea name="tweet" rows="2" cols="50"></textarea>
<!--
<input type="text" name="tweet">
-->
<button type="submit">ツイートする<img src="img/player_button08_plus.png"></button>
</div>
<?php endif; ?>
<?php while($row = $res->fetch(PDO::FETCH_ASSOC)): ?>
<article>
<header>
<?php echo htmlspecialchars($row["nickname"],ENT_QUOTES); ?>
</header>
<p><?php echo htmlspecialchars($row["tweet"],ENT_QUOTES); ?></p>
<div><?php echo $row["created"]; ?></div>
</article>
<?php endwhile; ?>
</div>
</body>
</html>