Return confirm function does not work - javascript

I generate some buttons dynamically with php like this:
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo "<tr>
<td><a href='updateproject.php?id=$row[ID]' class='btn btn-warning btn-mini'>
<i class='icon-white icon-pencil'></i>
</a>
<a href='deleterow.php?del=$row[ID]' onclick='return confirm('You want to delete this?');' class='btn btn-danger btn-mini'>
<i class='icon-white icon-remove'></i>
</a>
</td>
<td>{$row['Projectname']}</td>
<td>{$row['Personincharge']}</td>
<td>{$row['Description']}</td>
<td>{$row['CreationDate']}</td>
<td>{$row['Location']}</td>
</tr>\n";
}
?>
I have tried so many things but it doesn't work. No alert is coming. It just getting deleted...

Your slashes will be breaking as it'll think the statement ends at confirm('. Try this:
onclick=\"return confirm('You want to delete this?');\"

I would encapsulate the table in a form and use the following code.
<form onsubmit=" return window.confirm('Are you sure?');">
<?php
//Add button php here and fields
?>
</form>

<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$('.delete').click(function(event){
event.preventDefault();
confirm('You want to delete this?');
});
});
//db connection goes here
$query = mysql_query("Select * FROM `table_name`");
echo "<table>";
while( $row = mysql_fetch_assoc( $query ) ){
echo
"<tr>
<td> <a href='deleterow.php?del={$row['id']}' class='delete'>Delete</a>
</td>
<td>{$row['Projectname']}</td>
<td>{$row['Personincharge']}</td>
<td>{$row['Description']}</td>
<td>{$row['CreationDate']}</td>
<td>{$row['Location']}</td>
</tr>\n";
}
echo "</table>";

Related

HTML & PHP: Wrong button allignment and wrong output

So I have this comment system made with PHP and JS (dialogify) and I have two problems: one is obviously the buttons' allignment and the second is the output of the dialog. At the end of each line i get a <br> even though the line ends without going actually on a new line and the row won't update after pressing ok, I have to reload the page to actually see the updated content.
EDIT: Now the buttons are alligned
<div class="row">
<div class="col-sm-12 text-right">
<form method="post" action="">
<button class="btn btn-warning btn-sm" id="<?php echo $row['id']; ?>" value="Edit" type="button" onclick="edit_row('<?php echo $row['id']; ?>');" /><i class='fa fa-pencil'></i></button>
<button class="btn btn-danger btn-sm" name="delete_reply" type="submit"><i class='fa fa-trash'></i></button>
</form>
</div>
</div>
Here is the code I use to display the comments.
$query_reply = $mysqli->prepare("SELECT * FROM replies WHERE postID=?");
$query_reply = $mysqli->bind_param("i",$postid);
$query_reply ->execute();
$row = $query_reply->fetch_assoc();
<div id="content_val<?php echo $row['id']; ?>"><?php echo nl2br($row['reply']);?><br /></div>
The code for the insertion
if (isset($_POST['edit_row'])) {
$sql = $mysqli->prepare("UPDATE replies SET reply=? WHERE id=? AND postID=?");
$id = $_POST['id'];
$content = $_POST['content'];
$postid = $_SESSION['id'];
$sql->bind_param("sii", $content, $id, $postid);
if ($sql->execute()) {
$sql = $mysqli->prepare("SELECT * FROM replies WHERE postID='$id'");
$sql->bind_param("i", $postid);
$sql->execute();
}
}
EDIT 2: I managed to make the content update in real time by changing the action after $sql->execute();
if ($sql->execute()) {
print "success";
} else {
$error_message = "Problem in editing Record";
}

how to get value from php loop to div

i want to change div value when clicking php while loop.,
My php code
<?php $query = mysql_query("select * from tbl_sub_product where product_id='$id'");
while($row=mysql_fetch_array($query))
{ ?>
<div><a href="#" class="showElement" id="showElement" >
<?php echo $row['name']; ?><?php echo $row['id']; ?></a></div>
<?php } ?>
my query was
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
var $target = $('#target');
$(".showElement").click( function(e) {
e.preventDefault();
$target.append( $(this).text() );
});
});
</script>
i want the result in this div
<div id="target">user Id:<php echo $id ?>User Nmae:<?php echo $name ?>user designation :<?php echo $designation ?></div>
what changes i want to do my code for getting data in my second div
You're asking to take the mysql data from php and magically know what it is without querying again with AJAX or some other request.
There are many ways you can do this, but the simplest is to store the exact view of what you want in a child div below the a tag.
You need to change to prepared statements too.... If you don't want to fine just change the stmt stuff back to what you have and row will do what you want.
<style>
.hidden{
display: none;
}
</style>
<?php
$stmt = mysqli_prepare("select * from tbl_sub_product where product_id='$id'");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){
?>
<div>
<a href="#" class="showElement" >
<?php echo $row['name']; ?> <?php echo $row['id']; ?>
</a>
<div class="hidden getTarget">
user id: <php echo $row['id']; ?>
User name: <?php echo $row['name']; ?>
user designation: <?php echo $row['designation']; ?>
</div>
</div>
<?php
}
?>
<script>
$(document).ready(function(){
var $target = $('#target');
$(".showElement").click( function(e) {
e.preventDefault();
$target.append( $(this).parent().find('.getTarget').text() );
});
});
</script>
Don't use ids in a loop. Unless you have a specific identifier to separate each one.
Don't copy and paste as well.. please read the code and understand it.

Send parameter to the PHP-function from JavaScript

I've a such PHP-script:
<?php
$menuItemList = getSubPkgCategForDDList(echo "<script>showSubCatForMenuItem();</script>");
if(isset($menuItemList)){
foreach($menuItemList as $u){
?>
<p><span contenteditable="true"><?php echo $u->name ?></span><button type="button" class="btn btn-danger btn-xs" onclick="deleteCategory(<?php echo $u->pkg_cat_ddlist_id ?>)">Delete</button>
<button type="button" class="btn btn-success btn-xs" onclick="editCategory(<?php echo $u->pkg_cat_ddlist_id ?>,<?php echo "'".$u->name."'" ?>)">Save</button></p>
<?php
}
}
?>
Function getSubPkgCategForDDList must generate html-code,so it depends from parameter, which is send to this function.
I get this parameter from such js-function showSubCatForMenuItem():
function showSubCatForMenuItem(){
console.log($('#menuItem').val());
return $('#menuItem').val();
}
This function takes data from such dropdown list:
<select id="menuItem" onchange="showSubCatForMenuItem()">
<?php
$itemList = getPackCategoriesForAsideMenu();
if(isset($itemList)){
foreach($itemList as $u){
?>
<option value="<?php echo $u->pkg_cat_ddlist_id ?>"><?php echo $u->name ?></option>
<?php
}
}
?>
</select>
How to do that parameter transfer is correctly, when I load page and select item from dropdown list? Sorry for my English.
You should take advantage of $_SESSION in this case. Now print out the drop down :
<select id="menuItem">
<?php
$itemList = getPackCategoriesForAsideMenu();
if(isset($itemList)){
foreach($itemList as $u){
echo'<option value="'.$u->pkg_cat_ddlist_id.'">'.$u->name.'</option>';
}
}
?>
</select>
Write the JS script :
$("#menuItem").live('change',function(){
var val = $(this).val();
$.post('change.php',{data:val},function(){
// Do some
});
});
And create a php file named change.php :
<?php
session_start();
if(!empty($_POST['data'])){
$_SESSION['menu_sltd'] = (int) $_POST['data']; // It makes sure that the data sent is integer / number
}
?>
Now, change your main script to :
<?php
session_start();
$menu_sltd = (!empty($_SESSION['menu_sltd']) ? $_SESSION['menu_sltd'] : 'default id'); // Default id is the default menu id if it's blank
$menuItemList = getSubPkgCategForDDList($_SESSION['menu_sltd']);
if(isset($menuItemList)){
foreach($menuItemList as $u){
echo'
<p>
<span contenteditable="true">'.$u->name.'</span>
<button type="button" class="btn btn-danger btn-xs" onclick="deleteCategory('.$u->pkg_cat_ddlist_id.')">Delete</button>
<button type="button" class="btn btn-success btn-xs" onclick="editCategory('.$u->pkg_cat_ddlist_id.',\''.$u->name.'\')">Save</button>
</p>';
}
}
?>
GOOD LUCK,, glad to help you. Don't give up
In javascript function showSubCatForMenuItem() you can set the value of selected item in some hidden field on every change event the value selected by user will get updated, then while saving the use this value that is saved in the hidden field.

Pulsate not executed after click with PHP

I have a pulsate:
<script>
$(document).ready(function() {
$(".pane > a").click(function (event) {
event.preventDefault();
$(this).parent().effect("pulsate", { times:2 }).fadeOut('slow');
});
});
</script>
Then the PHP:
<?php
if($_SESSION['username']) {
if(isset ($_GET['id'])) {
mysql_query("DELETE FROM reviews WHERE id='". mysql_real_escape_string($_GET['id']) ."'");
}
}
$grab = mysql_query("SELECT * FROM reviews");
if (mysql_num_rows($grab)==0) {
echo ("<div class=''><strong>Sorry!</strong> No reviews have been created!</div>");
}
while($row = mysql_fetch_array($grab)){
?>
<div class="pane">
<img src="http://uploadir.com/u/6hmr4fr1" alt="delete" class="delete" />
<?php echo $row['comment'] ?>
</div>
It works but doesn't delete from the database. I'm working on an AJAX script, which works perfect, but it's something I'm not doing right here.
It pulsates out with:
<a href="reviews/editReview/<?php echo $row['id'] ?>"rel="noAJAX">
but when I take out the rel="noAJAX" it deletes the information from the database, which I want to happen, but it isn't pulsating then deleting.
Any ideas? I'm using jQuery UI 1.8.2 & jQuery 1.5

why is window.open php fetching variable is fetching same id every time and not the remaining entries?

Here in the code, $fetch['id'] working fine else where but in window.open it is taking same value always that is id number 25, and not the other ids, i can't understand why is it so....Please help........
$i=1;
while ($fetch = mysql_fetch_array($data_qry))
{
echo $url = $_SERVER['REQUEST_URI']."/edit_user_comment.php?id=".$fetch['id']; // here variable working fine....
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$fetch['comp_name']."</td>";
if (strlen($fetch['comp_add'])> 10)
{
echo "<td>".substr($fetch['comp_add'], 0, 10)."...";
}else{
echo "<td>".$fetch['comp_add'];
}"</td>";
echo "<td>".$fetch['business_registration_no']."</td>";
echo "<td>".$fetch['email_id']."</td>";
echo "<td>".$fetch['tel_no']."</td>";
echo "<td>".$fetch['fax']."</td>";
echo "<td>".$fetch['prsn_in_chrg']."</td>";
echo "<td>".$fetch['ph_no']."</td>";
?>
<script>
function windowopen(){
window.open('<?= $url; ?>','Comment Edit', 'menubar=1,location=1,status=1,scrollbars=yes,width=500, height=500');
//here it is takin $fetch['id']=25 only and not other ids, i want to know the mistake i m doing here... Please help
}
</script>
<?php
if($fetch['comments']!='')
{
echo "<td><textarea name='comments' id='id_comments'>".$fetch['comments']."</textarea>
<a onclick='return windowopen();' class='button button-primary button-large edit'>Edit</a></td>";
}else{
echo "<td><a onclick='return windowopen();' class='button button-primary button-large'>Add Comment</a></td>";
}
echo "<td> <a href='response.php?id=".$fetch['id']."&action=approve' class='button button-primary button-large approve'>Approve</a> <a href='response.php?id=".$fetch['id']."&action=dissapprove' class='button button-primary button-large'>Dissapprove</a> </td>";
echo "</tr>";
$i++;
}
To elaborate on my earlier comment - Generated links should look something like:
echo '<td><a onclick="return windowopen(\''.$url.'\');">Add Comment</a></td>';
...and your function should be declared once outside of the loop like :
<script>
function windowopen(url)
{
window.open(url,'Comment Edit', '/*blah*/');
}
</script>
This is because you are redeclaring the same function windowopen() so the last declaration overrides the others and you got the same function for all elements

Categories