So I am currently working on a forum, and I want to be able to have users edit their own questions and replies. Currently I am able to edit questions (sort of *other error is below) just fine. But replies are my biggest problem right now. Currently it shows an edit button under a reply but then it will allow me to edit ALL the other replies on the page and then when I save it, it saves the very last row.
<h3>Replies:</h3>
<div id="replies">
<?php
while($rows = mysqli_fetch_array($result2)) {
?>
<p id="dates"><?php echo $rows['a_datetime']; ?></p>
<div id="reply">
<b><p><?php echo $rows['a_username']; ?></p></b>
<?php
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls
$text = htmlspecialchars($rows['a_answer']);
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
$url = preg_replace("/^http:/i", "https:", $url);
// make the urls hyper links
echo preg_replace($reg_exUrl, '<a title="Opening this link will take you to a new page" alt="External Link Deleted" target="_blank" href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', '<p id="reply'.$rows['a_id'].'">'.$text.'</p>');
} else {
?>
<p id="reply<?php echo $rows['a_id']; ?>"><?php echo htmlspecialchars($rows['a_answer']); ?></p>
<?php
}
if($_SESSION['username'] == $rows['a_username']) {
$editReply = true;
?>
<div id="questionId"><?php echo $rows['question_id'];?></div>
<div id="replyId"><?php echo $rows['a_id'];?></div>
<button id="editReply">Edit</button>
<button id="saveReply">Save</button>
<button id="cancelReply">Cancel</button>
<script type="text/javascript">
$(document).ready(function(argument) {
$('#saveReply').hide();
$('#cancelReply').hide();
});
</script>
<?php
}
?>
</div>
<script type="text/javascript">
$(document).ready(function(argument) {
$('#editReply').click(function() {
$('#reply<?php echo $rows['a_id']; ?>').attr('contenteditable','true');
$('#reply<?php echo $rows['a_id']; ?>').css('background','white');
$('#reply<?php echo $rows['a_id']; ?>').css('border','solid 1px');
$('#saveReply').show();
$('#cancelReply').show();
$('#editReply').hide();
});
$('#saveReply').click(function() {
// Get edit field value
$detail = $('#reply<?php echo $rows['a_id']; ?>').html();
$q_id = $('#questionId').html();
$a_id = $('#replyId').html();
$.ajax({
url: 'editReply.php',
type: 'post',
data: {detail: $detail, q_id: $q_id, a_id: $a_id},
datatype: 'html',
});
$('#editReply').show();
$('#saveReply').hide();
$('#cancelReply').hide();
$('#reply<?php echo $rows['a_id']; ?>').attr('contenteditable','false');
$('#reply<?php echo $rows['a_id']; ?>').css('background','#D8D8D8');
$('#reply<?php echo $rows['a_id']; ?>').css('border','none');
});
$('#cancelReply').click(function() {
$('#editReply').show();
$('#saveReply').hide();
$('#cancelReply').hide();
$('#reply<?php echo $rows['a_id']; ?>').attr('contenteditable','false');
$('#reply<?php echo $rows['a_id']; ?>').css('background','#D8D8D8');
$('#reply<?php echo $rows['a_id']; ?>').css('border','none');
});
});
</script>
<?php
}
?>
editreply.php:
<?php
$host = "host"; // Host name
$user = "username"; // Mysql username
$password = ""; // Mysql password
$db_name = "db"; // Database name
$tbl_name = "fanswer"; // Table name
// Connect to server and select databse.
$conn = mysqli_connect($host, $user, $password)or die("cannot connect");
mysqli_select_db($conn, $db_name)or die("cannot select DB");
$detail = htmlspecialchars($_POST['detail']);
$q_id = $_POST['q_id'];
$a_id = $_POST['a_id'];
// Add your validation and save data to database
echo $detail;
echo $q_id;
echo $a_id;
$sql = "UPDATE $tbl_name SET a_answer = '$detail' WHERE question_id='$q_id' AND a_id= '$a_id'";
$result = mysqli_query($conn, $sql);
?>
Originally I had all the editable content in divs but for some reason it made some pages load funny so for now I was hoping to use the p tag.
How can I make it so only one of the replies are editable and so it only sends that information to the editreply.php script?
*My other problem which is sort of a side problem, when I go to edit something that has a link in it I get a load of gibberish posted into my database. E.g. a user post LMAO: https://afunnypic.com, the information in my database says:
LMAO: <a title="Opening this link will take you to a new page" alt="External Link Deleted" target="_blank" href="https://afunnypic.com" rel="nofollow">https://afunnypic.com</a><br>
Which I don't understand.
Just found my answer to the first problem, all I did was move the script inside the if ($editReply = true) statement and it worked!
If anyone can help with the second bit on editing the links bit that would be great!
Related
I am having problems implementing my $myimage into my echos for session.My goal is to put the $myimage into the echo but idk how to do it so i am hoping someone who comes across my post does.I have a gallery of photos and at the moment when i click on something it doesnt send the photo i clicked on rather on the last photo i added.
<?php
while ($row = mysqli_fetch_assoc($result)) {
$myimage="".$row['image']."";
$_SESSION['myimage'] = $myimage;
echo "<div class='img-single'>
<a href='images/fpage.php'><img class='i' src='images/".$row['image']."'>
</a><figcaption class='figcaption'>".$row['image_text']."</figcaption>
</div>";
}
?>
trying to make something look like this
<?php
while ($row = mysqli_fetch_assoc($result)) {
//$myimage="".$row['image']."";
$_SESSION['myimage'] = $myimage;
echo "<div class='img-single'>
<a href='images/fpage.php'><img class='i'
src='images/$myimage="".$row['image']."";'>
</a><figcaption class='figcaption'>".$row['image_text']."</figcaption>
</div>";
}
?>
I'm making a quiz website, for that questions and answers will be fetched from database and using PHP and MySQL.
I've stored the options in one single column using JSON.(Stored as string array) Question is displaying correctly but options couldn't; I've written them in similar manner.
quiz.php
echo '
<div id="ques_block">
<br>
<p id="ques"></p>
';
for($i=0; $i<$_SESSION["opt_size"]; $i++)
echo '<p class="opt" id="opt'.$i.'"></p>';
echo '
<div id="nav-for"><img src="next.png" alt="Next =>"></div>
</div>
';
next.php will be called using AJAX when NEXT is clicked.
next.php
$sql = "SELECT QUESTION, ANSWER, OPTIONS FROM user_quiz WHERE CUST_NO=$cust_no AND QUES_NO=$new_ques_no;";
$res = mysqli_query($conn, $sql) or die("bad Query: $sql");
if(mysqli_num_rows($res)==0)
echo "END";
else
{
$row = mysqli_fetch_assoc($res);
$ques = $row["QUESTION"];
$opts = $row["OPTIONS"];
$opt = json_decode($opts);
$resRows = sizeof($opt);
echo '
<script type="text/javascript">
document.getElementById("ques").innerHTML="'.$ques.'";
</script>
';
for($i=0; $i<$resRows; $i++)
echo '<script type="text/javascript">
document.getElementById("opt'.$i.'").innerHTML = "'.$opt[$i].'";
</script>
';
}
Edited:
#ADyson I'll try your method, Thanks,
BTW This is AJAX code as requested:
$(document).ready(function(){
$("#nav-for").click(function(){
$.post("nextQues.php",
{custNo: $("#custNo").val(), quesNo: $("#quesNo").val()},
function(data){
$("#ques").html(data);
}
);
});
});
Good Day!
I'd like to ask something. I have a link named "delete" at the index file. Now, I want to confirm from a user before doing the delete operation. However, I'd like to put the script for confirmation to the delete file. I've tried multiple ways, yet it's useless. But it can delete actually, my problem exactly is that, the confirmation box does not prompt. Kinda' stock in here. Please help.
Heres my code at index.php (link to delete):
`<p><strong>Country: </strong> <?php echo $country['countries'];?>   |
  Edit |
<a href="deletecountry.php?id=<?php echo $country['id']; ?>"
class="confirmation">Delete</a></p>`
And here's my delete.php file:
`<?php
require 'dbconnect.php';
$id = $_GET['id']; ?>
<script>
function confirmationDelete(){
var conf = confirm('Are you sure want to delete this record?');
if(conf==true){
<?php
$sql = "DELETE countries FROM countries WHERE id=$id";
if (mysqli_query($connect, $sql)) { ?>
alert("Record Deleted!");
<?php }
else { ?>
alert("Error Deleting Record!");
<?php } ?>
}
return conf;
}
</script>`
in
Delete
then in your deletecountry.php
<?php
session_start();
require 'dbconnect.php';
$id = $_GET['id']; ?>
$sql = "DELETE countries FROM countries WHERE id=$id";
if (mysqli_query($connect, $sql)) {
$_SESSION['msg'] = 'Successfully Deleted';
}
else {
$_SESSION['msg'] = 'Error Deleting';
}
// redirect to somewhereelse.php
then in your somewhereelse.php
<?php
session_start();
if(isset($_SESSION['msg']))
{
echo '<script>alert("'. $_SESSION['msg'] .'")</script>';
session_unset('msg');
}
?>
<a onclick='javascript:confirmationDelete($(this));return false;' href="delete.php?id=1">Delete</a>
and create javascript function
function confirmationDelete(anchor)
{
var conf = confirm('Are you sure want to delete this record?');
if(conf)
window.location=anchor.attr("href");
}
or
delete
I'm new in programming and I'm stuck with dropdown lists.
I have to display 2 dropdown lists. In first one I have to display all categories from one table. In second one I have to display values from another table depends of the selected value in first dropdown list.
I found this answer, but I don't know how can I get selected value and use it for second dropdown list. I tried with $_POST, but it doesn't work.
Can someone give me any instructions how to do that?
Thank you.
<?php
...
$conn = new mysqli($hostname, $username, $password, $databaseName)
or die ('Cannot connect to db');
$kat = $conn->query("select idkategorija, kategorija from kategorija");
echo "<html>";
echo "<body>";
echo "<select name='izb_kategorija' method = 'post'>";
while ($row = $kat->fetch_assoc()) {
unset($idkategorija, $kategorija);
$idkategorija = $row['idkategorija'];
$kategorija = $row['kategorija'];
echo '<option value="'.$idkategorija.'">'.$kategorija.'</option>';
}
echo "</select>";
echo $idkategorija; // I want to use idkategorija in my next query for second dropdown list
echo "</body>";
echo "</html>";
?>
I modified this code for you.Use this code,i hope it will work for you.
<?php
...
$conn = new mysqli($hostname, $username, $password, $databaseName);
or die ('Cannot connect to db');
$kat = $conn->query("select idkategorija, kategorija from kategorija");
?>
<html>
<body>
<select name='izb_kategorija' method = 'post'>
<?php
while ($row = $kat->fetch_assoc())
{
$idkategorija = $row['idkategorija'];
$kategorija = $row['kategorija'];
?>
<option value="<?php echo $idkategorija; ?>"><?php echo $kategorija; ?></option>
<?php
}
?>
</select>
</body>
</html>
I have a PHP snippet that generates a table and fills it, and adds a delete button at the end of each row.
while($row = mysql_fetch_array($result)){
$num=$row['id'];
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['adress']."</td>";
echo "<td>".$row['phonenumber']."</td>";
echo "<td><form action='delete.php' method='post'><button type='submit' value=$num name='deleteId'>delete</button></form></td>";
echo "</tr>";
}
The delete.php file is this one :
<?php
$host="localhost";
$username="root";
$password="";
$db_name="students";
mysql_connect("$host", "$username", "$password");
mysql_select_db("$db_name");
$id = $_POST['deleteId'];
$sql="DELETE FROM students WHERE id='$id'";
$result=mysql_query($sql);
?>
I want to do this using Ajax asynchronously, ie. I don't want my page to refresh. Tried a million ways, yet it fails each time. Thanks in advance.
Instead of using a form, you need to write your own JavaScript to handle the server call. Here's one way to do it. Make your PHP look something like this:
while($row = mysql_fetch_array($result)){
$num=$row['id'];
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['adress']."</td>";
echo "<td>".$row['phonenumber']."</td>";
echo "<td><button onclick="deleteStudent($num)">delete</button></td>";
echo "</tr>";
}
And then have a JS function that looks something like this:
function deleteStudent(studentId) {
$.ajax({
url: "delete.php",
method: 'post',
data: {
deleteId: studentId
}
});
}
Another way:
Firstly, assign a unique ID for each of the delete button.
while($row = mysql_fetch_array($result)){
$num = $row['id'];
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['adress']."</td>";
echo "<td>".$row['phonenumber']."</td>";
echo "<td><button id='delete-" . $row['id'] . "'>delete</button></td>";
echo "</tr>";
}
Then use jQuery:
$('button[id^="delete"]').click(function () {
var id = $(this).attr('id').substr(6);
$.ajax({
type: "POST",
url: "delete.php",
data: {deleteId: id}
});
});