Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13513054 - Chairuni Aulia Nusapati #71

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0e9c0c5
My First Commit Attempt
chairuni-an Oct 8, 2015
8b8b619
My Second Commit Attempt
chairuni-an Oct 8, 2015
c801b72
My Third Commit Attempt
chairuni-an Oct 14, 2015
2a71962
My Third Commit Attempt
chairuni-an Oct 14, 2015
be06fa6
My Fifth Commit Attempt
chairuni-an Oct 14, 2015
3d34017
My Sixth Commit Attempt
chairuni-an Oct 14, 2015
637da8f
My Seventh Commit Attempt
chairuni-an Oct 14, 2015
d76f687
My Eight Commit Update
chairuni-an Oct 14, 2015
b7e6e43
My Ninth Commit Attempt
chairuni-an Oct 14, 2015
45aef08
My Tenth Commit Attempt
chairuni-an Oct 14, 2015
84b9aa9
My Eleventh Commit Attempt
chairuni-an Oct 15, 2015
fb02c30
Separated style & script file
chairuni-an Oct 16, 2015
8dcd2dd
created delete_question.php, created separated script file
chairuni-an Oct 16, 2015
511465f
Starting to make AJAX to make up down vote,
chairuni-an Oct 16, 2015
81b6837
Added php file to update vote in database, connect all the files
chairuni-an Oct 16, 2015
273c089
Deleted unused codes, changed buttons into a href
chairuni-an Oct 16, 2015
0e7d27b
added answer form validation, added form & button styling,
chairuni-an Oct 16, 2015
4fe0f70
Added AJAX vote for question, improved code indentation in answer.php
chairuni-an Oct 16, 2015
f4310be
Added Search function and fixed problem with input with special chara…
chairuni-an Oct 16, 2015
506f845
Added time and content trimming for view
chairuni-an Oct 16, 2015
09579f9
Fix problems with input with special characters, added link to main page
chairuni-an Oct 16, 2015
db1ef5d
Added sql dump
chairuni-an Oct 16, 2015
a4225f3
Modified readme, changed file name
chairuni-an Oct 16, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,22 @@ Pengguna dapat mencari pertanyaan dengan melakukan search ke `judul` maupun `isi

### Penjelasan Teknis

`Silakan isi bagian ini dengan penjelasan anda, sesuai Petunjuk Pengerjaan di atas.`
**Validasi pada client side**
Validasi pada client side dilakukan menggunakan javascript
Pertama, saat user mengklik tombol post, fungsi validateQuestion() atau validateAnswer() akan dipanggil.
Fungsi validateQuestion() atau validateAnswer() digunakan untuk melakukan validasi masukan.
Akan mengembalikan true jika masukan valid, mengeluarkan alert jika tidak.
Fungsi validateQuestion() atau validateAnswer() pertama akan mengecek apakah ada field yang kosong, jika ada maka masukan tidak valid.
Kemudian fungsi tersebut akan memanggil fungsi validateEmail() yang bertugas untuk validasi format email menggunakan regex.

**Menggunakan AJAX untuk menambah dan mengurangi vote**
Saat pengguna mengklik tombol tambah atau kurang vote, fungsi
addQuestionVote(s), subtractQuestionVote(s), addAnswerVote(s), atau subtractAnswerVote(s)
akan dipanggil.
Fungsi tersebut akan melakukan sebuah request.
Kemudian sebuah php akan dijalankan untuk mengubah data vote di database.
Kemudian fungsi tersebut akan menerima hasil request, yaitu data vote yang sudah diubah.
Kemudian fungsi akan mengubah nilai elemen yang berisi vote pada view file, dengan nilai vote yang sudah diubah.

### Knowledge

Expand Down
160 changes: 160 additions & 0 deletions Simple StackExchange/StackExchange.sql

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions Simple StackExchange/addansvote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php include 'connect.php';?>

<?php
// Update database
$answer_id = $_REQUEST["answer_id"];
$sql = "UPDATE Answer SET vote = (vote + 1) WHERE answer_id='$answer_id'";
$conn->query($sql);
$sql = "SELECT vote FROM Answer where answer_id='$answer_id'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if (mysqli_num_rows($result) > 0)
echo $row["vote"];
exit;
?>
14 changes: 14 additions & 0 deletions Simple StackExchange/addquestionvote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php include 'connect.php';?>

<?php
// Update database
$question_id = $_REQUEST["question_id"];
$sql = "UPDATE Question SET vote = vote + 1 WHERE question_id='$question_id'";
$conn->query($sql);
$sql = "SELECT vote FROM Question where question_id='$question_id'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if (mysqli_num_rows($result) > 0)
echo $row["vote"];
exit;
?>
31 changes: 31 additions & 0 deletions Simple StackExchange/anspost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>

<head>
<title>Simple StackExchange</title>
</head>

<body>

<?php include 'connect.php';?>

<?php
$question_id = $_POST['question_id'];
$name = mysqli_real_escape_string ($conn, $_POST["name"]);
$email = $_POST["email"];
$content = mysqli_real_escape_string ($conn, $_POST["content"]);


$sql = "INSERT INTO Answer (question_id, name, email, content)
VALUES ('$question_id', '$name', '$email', '$content')";
if (mysqli_query($conn, $sql)) {
echo $question_id;
header('Location: answer.php?question_id=' . $question_id);
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

?>

</body>
</html>
150 changes: 150 additions & 0 deletions Simple StackExchange/answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href='style.css'/>
<script type="text/javascript" src="script.js"></script>
<title>Simple StackExchange</title>
</head>

<body>

<?php include 'connect.php';?>


<div class="link-normalizer"><a class='title' href="question.php">Simple StackExchange</a></div>
<br>
<br>
<br>
<br>



<?php
$border =
"<hr class='line'>"
;
if (isset($_GET['question_id'])){
//for the question
$question_id = mysqli_real_escape_string($conn, $_GET["question_id"]);
$sql = "SELECT question_id, vote, topic, content, email, time FROM Question WHERE question_id = $question_id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
echo
"<div class='subtitle'>"
.$row['topic']
."</div>"
."<hr class='line'>"
."<div class='block-QA'>"
."<div class='bQA-vote'>"
."<div class='vote-up' onclick='addQuestionVote(".$row['question_id'].")''>"
."</div>"
."<br>"
."<a class='vote-value' id='question_vote".$row['question_id']."'>"
.$row['vote']
."</a>"
."<br><br>"
."<div class='vote-down' onclick='subtractQuestionVote(".$row['question_id'].")''>"
."</div>"
."</div>"
."<div class='bQA-content'>"
.$row['content']
."<br><br>"
."</div>"
."<div class='bQA-identity'>"
."asked by "
.$row['email']
." at "
.$row['time']
." | "
."<a id='color-orange' href=# onclick='editconfirm(".$row['question_id'].")'" ."'>"
."edit"
."</a>"
." | "
."<a id='color-red' href=# onclick='deleteconfirm(".$row['question_id'].")'"."'>"
."delete"
."</a>"
."</div>"
."</div>"
;

echo "<br><br><br><br><br>";

//for the answer count
$sql = "SELECT count(*) AS answer_count FROM Answer
WHERE question_id = $question_id";
$result = mysqli_query($conn, $sql);

$row = mysqli_fetch_assoc($result);
if ($row['answer_count'] > 1){
$answer = " Answers";
}
else{
$answer = " Answer";
}
$answer_count =
"<div class='subtitle'>" . $row['answer_count'] . $answer . "</div>"
;
echo $answer_count . $border;
//for the answer
$sql = "SELECT answer_id, vote, content, email, time FROM Answer
WHERE question_id = $question_id";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo
"<div class='block-QA'>"
."<div class='bQA-vote'>"
."<div class='vote-up' onclick='addAnswerVote(".$row['answer_id'] . ")''>"
."</div>"
."<br>"
."<a class='vote-value' id='answer_vote" . $row['answer_id'] . "'>" .$row['vote']
."</a>"
."<br><br>"
."<div class='vote-down' onclick='subtractAnswerVote(" . $row['answer_id'] . ")''>"
."</div>"
."</div>"
."<div class='bQA-content'>"
.$row['content']
."<br><br>"
."</div>"
."<div class='bQA-identity'>"
."answered by "
.$row['email']
." at "
.$row['time']
."</div>"
."</div>"
."<hr class='line'>"
;
}
}

echo "<br><br>";

//answer form
$YA =
"<div class='subtitle'>" . "<a id='color-grey'>" . "Your Answer" . "</a>" . "</div>"
;
$form =
"<form name='answerForm' action='anspost.php' onsubmit='return validateAnswer()' method='post'>
<input type='hidden' name='question_id' value=' " . $question_id ."'>
<input type='text' class='form-text' name='name' placeholder='Name'><br>
<input type='text' class='form-text' name='email' placeholder='Email'><br>
<textarea class='form-textarea' name='content' placeholder='Content'></textarea><br>
<button class='button-post' type='submit'> Post </button>
</form>";
echo $YA . $form;


}
else{
echo "An error occured, the question is not available";
}
?>

</body>
</html>
57 changes: 57 additions & 0 deletions Simple StackExchange/ask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href='style.css'/>
<script type="text/javascript" src="script.js"></script>
<title>Simple StackExchange</title>
</head>
<body>
<div class="link-normalizer"><a class='title' href="question.php">Simple StackExchange</a></div>
<br>
<br>
<br>
<br>
<div class="subtitle">What's your question?</div>
<hr class='line'>

<?php include 'connect.php';?>

<?php
if (isset($_GET['question_id'])){
$question_id = mysqli_real_escape_string($conn, $_GET["question_id"]);
$sql = "SELECT name, email, topic, content FROM Question WHERE question_id = $question_id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if (mysqli_num_rows($result) > 0) {
$name = $row["name"];
$email = $row["email"];
$topic = $row["topic"];
$content = $row["content"];
}
else{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
else{
$question_id = 0;
$name = "";
$email = "";
$topic = "";
$content = "";
}

echo
"<form name=\"askForm\" action=\"askpost.php\" onsubmit=\"return validateQuestion()\" method=\"post\">
<input value=\"" . $question_id . "\" type=\"hidden\" name=\"question_id\">
<input value=\"" . $name . "\" type=\"text\" class='form-text' name=\"name\" placeholder=\"Name\"><br>
<input value=\"" . $email . "\" type=\"text\" class='form-text' name=\"email\" placeholder=\"Email\"><br>
<input value=\"" . $topic . "\" type=\"text\" class='form-text' name=\"topic\" placeholder=\"Question Topic\"><br>
<textarea name=\"content\" class='form-textarea' placeholder=\"Content\">" . $content ."</textarea><br>
<button class='button-post' type='submit'> Post </button>
</form>";
?>
</body>
</html>


48 changes: 48 additions & 0 deletions Simple StackExchange/askpost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>

<head>
<title>Simple StackExchange</title>
</head>

<body>

<?php include 'connect.php';?>

<?php
$question_id = $_POST["question_id"];
$name = mysqli_real_escape_string ($conn, $_POST["name"]);
$email = $_POST["email"];
$topic = mysqli_real_escape_string ($conn, $_POST["topic"]);
$content = mysqli_real_escape_string ($conn, $_POST["content"]);

if($question_id>0){
$sql = "UPDATE Question
SET name = '$name', email = '$email', topic = '$topic', content = '$content'
WHERE question_id = '$question_id'
";
if (mysqli_query($conn, $sql)) {
header('Location: answer.php?question_id=' . $question_id);
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

}
else{
$sql = "INSERT INTO Question (name, email, topic, content)
VALUES ('$name', '$email', '$topic', '$content')";
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
header('Location: answer.php?question_id=' . $last_id);
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}



?>


</body>
</html>
22 changes: 22 additions & 0 deletions Simple StackExchange/connect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<body>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "StackExchange";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Connected successfully :)
?>

</body>
</html>
26 changes: 26 additions & 0 deletions Simple StackExchange/delete_question.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>

<head>
<title>Simple StackExchange</title>
</head>

<body>
<?php include 'connect.php';?>

<?php
$question_id = mysqli_real_escape_string($conn, $_GET["question_id"]);

$sql = "DELETE FROM question
WHERE question_id=$question_id";
if (mysqli_query($conn, $sql)) {
echo "Alhamdulillah";
header('Location: question.php');
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

?>

</body>
</html>
Loading