show my result in a selected div - php

I have this form:
<FORM id="frmVote" action="../vote_dir/proccess.php" method="post">
<table id="tblMain" align="center">
<tr>
<td class="header"></td>
</tr>
<tr>
<td>
<?php
include "../vote_dir/loadpoll.php";
?>
</td>
</tr>
<tr>
<td>
<input id="votefor" name="votefor" type="hidden"/>
</td>
</tr>
<tr>
<td class="button">
<INPUT class="btnVote" onclick="return confirmSubmit()" type="submit" value="vote"/>
</td>
</tr>
<tr>
<td class="footer"></td>
</tr>
</table>
</FORM>
and this is part of the proccess.php:
<?php
$votefor = $_POST["votefor"];
//Get the IP of the user
$domain = $_SERVER["REMOTE_ADDR"];
$today = date("m/d/Y");
echo "<table id=\"tblResults\" align=\"center\">";
//MORE CODE
// Generate the results table
$doc = new DOMDocument();
$doc->load("../vote_dir/xml/results.xml");
$pollitems = $doc->getElementsByTagName("pollitem");
foreach( $pollitems as $pollitem )
{
$entries = $pollitem->getElementsByTagName("entryname");
$entry = $entries->item(0)->nodeValue;
$votes = $pollitem->getElementsByTagName("votes");
$vote = $votes->item(0)->nodeValue;
$tempWidth = $vote / $maxvotes;
$tempWidth = 300 * $tempWidth;
$votepct = round(($vote / $maxvotes) * 100);
echo "<tr><td width=\"45%\" class=\"polls\">$entry</td>";
echo "<td width=\"35%\" class=\"resultbar\"><div class=\"bar\" style=\"background-color: ";
getRandomColor();
echo "; width: $tempWidth px;\">$votepct%</div></td><td class=\"each_vote\" width=\"20%\">($vote votes)</td></tr>";
}
echo "<tr><td width=\"45%\" class=\"total\" colspan=\"3\">Σύνολο ψήφων: $maxvotes</td>";
echo "</table>";
?>
Like it is now it works fine. but I am trying to make it load my results in a specific div instead of opening the results like
I am trying to use this jquery (that I used in other things), it is working partialy since it opens the results in the div but it seems it doesn't sent the form into the proccess.php since I can see the result page without any change or message e.g "You have already voted".
.delegate('.btnVote', 'click', function(){
var keyValues = {
votefor : $(this).parent().find('input[name="votefor"]').val()
};
$.post('../vote_dir/proccess.php', keyValues, function(rsp){
$('#voting').load('../vote_dir/results.php');
});
return false;
})
this is my loadpoll.php
<?php
// Load the results xml file
$doc = new DOMDocument();
$doc->load("../vote_dir/xml/results.xml");
$root = $doc->getElementsByTagName("results")->item(0);
$question = $root->getAttribute("question");
echo "<table id=\"tblPoll\" align=\"center\"><tr><td class=\"question\">$question</td></tr>";
echo "<tr><td class=\"pollitem\">";
$pollitems = $doc->getElementsByTagName("pollitem");
$id = 1;
// Loop through each item, and create a radio button for each item
foreach( $pollitems as $pollitem )
{
$entries = $pollitem->getElementsByTagName("entryname");
$entry = $entries->item(0)->nodeValue;
$votes = $pollitem->getElementsByTagName("votes");
$vote = $votes->item(0)->nodeValue;
if ($id==1)
echo "<input id=\"entry$id\" class=\"radiobutton\" onclick=\"setVote('$entry')\" type=\"radio\" name=\"poll\" value=\"$entry\">$entry<br>";
else
echo "<input id=\"entry$id\" onclick=\"setVote('$entry')\" type=\"radio\" name=\"poll\" value=\"$entry\">$entry<br>";
$id = $id + 1;
}
echo "</td></tr>";
echo "</table>";
?>

$('.btnVote').live('click', function () {
$.post('../vote_dir/savevote.php', { votefor: $.trim($(this).parents('form').find('input[name="votefor"]').val()) }, function (data) {
$('#voting').html(data);
//Or
//$('#voting').load('../vote_dir/results.php');
});
return false;
});

Try changing the
$(this).parent().find('input[name="votefor"]').val()
to
$('#votefor').val()
I think the "$(this).parent()" leads to the <td>

Try this:
var data = $(this).parent().find('input[name= votefor]').val();
or
$data = $(this).parent().find('input[name= votefor]').val();

Related

jQuery load() not displaying echo table from php | wordpress

I'm using Wordpress and I want to display a table, which contains pictures and buttons. This table should get loaded with a jquery load() method and put the result into a div.
I get the result, which I want, but it doesn't display in the div. Just here:
here.
When I just put, for example, a H2 with text and without any php into the php file, then it gets displayed.
Script
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
jQuery(document).ready(function(){
var $id = GetURLParameter('id');
var urlGetItem = "getItems.php/?id=" + $id;
jQuery('#load_surveys').load(urlGetItem);
});
</script>
HTML
<div id="load_surveys"></div>
PHP
<?php
include_once 'db.php';
require ('../wp-blog-header.php');
global $wpdb;
global $current_user;
get_currentuserinfo();
$userId = $current_user->ID;
$id = $_GET['id'];
$result = mysqli_query($conn,"SELECT * FROM f5_users JOIN COMPARISONFOLDER ON COMPARISONFOLDER.USER_ID = f5_users.ID JOIN ITEM ON ITEM.COMPARISONFOLDER_ID LIKE COMPARISONFOLDER.COMPARISONFOLDER_ID WHERE COMPARISONFOLDER.COMPARISONFOLDER_ID LIKE $id AND f5_users.ID LIKE $userId");
echo "<table border='1'>
<tr>
<th>Item</th>
<th>Delete</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td><img src="data:image/jpeg;base64,'.base64_encode($row['ITEM']).'" width="500" height="auto"/></td>';
echo "<td><button type='button' onclick='deleteItem(".$row['ITEM_ID'].", $id)'>Testing Script</button></td>";
echo "</tr>";
}
echo "</table>";
?>
What do you get if you make console.log('id',$id) and console.log('url,urlGetItem)
jQuery(document).ready(function(){
var $id = GetURLParameter('id');
console.log('id',$id);
var urlGetItem = "getItems.php/?id=" + $id;
console.log('url,urlGetItem);
jQuery('#load_surveys').load(urlGetItem);
});
PHP
$markup = "<table border='1'>
<tr>
<th>Item</th>
<th>Delete</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$markup .= "<tr>";
$markup .='<td><img src="data:image/jpeg;base64,'.base64_encode($row['ITEM']).'" width="500" height="auto"/></td>';
e "<td><button type='button' onclick='deleteItem(".$row['ITEM_ID'].", $id)'>Testing Script</button></td>";
$markup .= "</tr>";
}
$markup .= "</table>";
echo $markup;

PHP explode function from checkboxes

I have a form where I'm submitting some values (from checkboxes) to another page and on that page I'm using an explode function to break the string inside the array. But I'm getting an additional (+1) value when I put the count() function on the explode.
HTML
<form name = "view" method = "POST" action ="cart.php">
<table align = 'center' width = '100%' border = '4'>
<tr>
<td colspan = '20' align = 'center'><h2> Viewing all the Products </h2></td>
</tr>
<tr align = 'center'>
<th>Item ID</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
</tr>
<tr align = 'center' class = "odd">
<?php
while($row = mysql_fetch_array($run))
{
$i_id = $row['item_id'];
$i_name = $row['item_name'];
$i_price = $row['item_price'];
?>
<td><?php echo $i_id; ?></td>
<td><?php echo $i_name; ?></td>
<td><?php echo $i_price; ?></td>
<?php
$item = $i_name ." ". $i_price;
?>
<td><input type="checkbox" name="addcart[]" value="<?php echo $item; ?>" onClick="return KeepCount()" />Tick</td>
</tr>
<?php }?><input type = "hidden" name = "check">
<button type= "submit" onclick = "location.href = 'cart.php';" id = "cart">Add to Cart</button> <?php } ?>
</table>
</form>
PHP (on page 2)
$prd = implode(",",$_POST['addcart']);
$final = explode(",", $prd);
for($i=0; $i<=count($final); $i++)
{
echo count($final); //this is where I'm getting the +1 to original count and hence everything falls apart.
echo $final[$i];
}
Note: I have include all the essential files like config.php and everything in the PHP file already.
Why that much extra code, directly do like below:-
foreach($_POST['addcart'] as $val){
echo $val;
}
Since count() starts counting from 1 and for loop starting from 0.
Replace this line:
for($i=0; $i<=count($final); $i++)
with
for($i=0; $i<=count($final)-1; $i++)

fail to manage checkbox using Pagination php using cookies

I want to manage pagination by using cookies.
I need to get values from selected checkboxe's and pass that values through pagination witch is done by php/mysql. And if on another page user select other checkboxes add their values to array of earlier seleced. For that i wrote one code but it did not work..What wrong with my code????
Following file is the manual.php file.
<script type="text/javascript" src="paginate.js"> </script>
<tr>
<td><strong>Select</strong></td>
<td width="59"><strong>Sr No.</strong></td>
<td width="300"><strong>Question </strong></td>
<td width="30"><strong>CorrectAnswer </strong></td>
<td width="50"><strong>Catagory </strong></td>
<td><strong>View Details</strong></td>
</tr>
<?php
include("config.php");
$start = 0;
$per_page = 10;
//$tablename = "testlist";
$targetpage = "manual.php?id=42";
if(!isset($_GET['page'])){
$page = 1;
} else{
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
$sql="select * from math order by id";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = $num_rows / $per_page;
$sql .= " LIMIT $start, $per_page";
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result))
{ ?>
<tr>
<td><input name="<?php echo $row['id'];?>" type="checkbox" class="remember_cb"></td>
<td><center><?php echo htmlspecialchars_decode($row['id'], ENT_QUOTES); ?></center>
</td>
<td style="padding-left:25px;margin:15px"><?php echo $_SESSION['question']=
htmlspecialchars_decode($row['question'], ENT_QUOTES); ?></center>
</td>
<td><center><?php echo htmlspecialchars_decode($row['correctAnswer'], ENT_QUOTES); ?>
</center></td>
<td><center><?php echo htmlspecialchars_decode($row['category'], ENT_QUOTES); ?>
</center></td>
<td><a href="<?php echo "manual_update.php?id=".$row['id']."&mode=update";?
>"id="dialog_10">View</a></td>
</tr>
<?php } ?>
<tr>
<td><strong>Select </strong></td>
<td><strong>Sr No </strong></td>
<td><strong>Question </strong></td>
<td><strong>CorrectAnswer </strong></td>
<td><strong>Category </strong></td>
<td><strong>View Details</strong></td>
</tr>
<?php
if($page > 1){
$prev = $page - 1;
$prev = " <a href='$targetpage&page=$prev'>prev</a> ";
} else {
$prev = "";
}
if($page < $num_pages){
$next = $page + 1;
$next = " <a href='$targetpage&page=$next'>next</a> ";
}
else
{
$next = "";
}
echo $prev;
echo $next;
?>
paginate.js
var aa_checkbox;
function init_checkbox(){
//setup blank cb cookie
if(!Cookie.read('checkbox'))
{
Cookie.write('checkbox', JSON.encode({}));
}
//setup "associative array" to match what is currently in the cookie
aa_checkbox = JSON.decode(Cookie.read('checkbox'));
//set up each checkbox with class="remember_cb"
$$('input.remember_cb').each(function(el){
//mark checked if it is in the cookie
if(aa_checkbox[el.name]){
el.checked = 'checked'
}
//setup onclick event to put checkbox status in the
if(el.checked){
aa_checkbox[el.name] = 1;
}else{
delete(aa_checkbox[el.name]);
}
})
})
//save aa_checkbox back into cookie upon leaving a page
window.onbeforeunload = function(){Cookie.write('cb',
JSON.encode(aa_checkbox));};
setup_form();
return true;
}
function setup_form(){
//set up form so that it adds the inputs upon submit.
$$('form.remember_cb_form').each(function(form){
form.addEvent('submit', function(ev){
//clean up previously inserted inputs
var aa_hidden_insert = $$('input.hidden_insert');
$each(aa_hidden_insert, function(el){
el.parentNode.removeChild(el);
})
var el_form = this;
//insert hidden elements representing the values stored in aa_checkbox
$each(aa_checkbox, function(i_value, s_name){
if(i_value){
var el_input = document.createElement('input');
el_input.type = 'hidden';
el_input.value = i_value;
el_input.name = s_name;
el_input.setAttribute('class', 'hidden_insert');
el_form.appendChild(el_input);
}
});
});
});
}
window.addEvent('domready', init_checkbox);
function selectall()
{
var selectAll = document.forms[0].length;
if(document.forms[0].topcheckbox.checked == true)
{
for(i=1;i<selectAll;i++)
{
document.forms[0].elements[i].checked = true;
}
}
else
{
for(i=1;i<selectAll;i++)
document.forms[0].elements[i].checked = false;
}
}
function goSelect()
{
var select = document.forms[0].length;
var checkboxes=""
for(i=1;i<select;i++)
{
if(document.forms[0].elements[i].checked==true)
checkboxes+= " " + document.forms[0].elements[i].name
}
if(checkboxes.length>0)
{
var con=confirm("You have selected ...........");
if(con)
{
window.location.assign("submit.php?recsno="+checkboxes)
}
}
else
{
alert("No record is selected.")
}
}

Show all rows in mysql table then give option to delete specific ones

I want to have the ability to show all the entries in a database table and by each one give the user the ability to delete specific ones.
I am currently using a for each loop that loops through the database showcasing each entry.
$result = mysql_query("SELECT * FROM KeepScores");
$fields_num = mysql_num_fields($result);
echo "<table><tr>";
// printing table headers
echo "<td>Recent Posts</td>";
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
How would to add a delete button that appears by each one and removes the entry from the database table?
You can do it with forms:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr>
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<form action="delete.php" method="post">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endwhile; ?>
</table>
//delete.php:
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
header('Location: main.php');
}
Or you can do it with jQuery and AJAX:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr id="<?php echo $row['id']; ?>">
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<button class="del_btn" rel="<?php echo $row['id']; ?>">Delete</button>
</td>
</tr>
<?php endwhile; ?>
</table>
<script>
$(document).ready(function(){
$('.del_btn').click(function(){
var del_id = $(this).attr('rel');
$.post('delete.php', {delete_id:del_id}, function(data) {
if(data == 'true') {
$('#'+del_id).remove();
} else {
alert('Could not delete!');
}
});
});
});
</script>
//delete.php
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
if($result !== false) {
echo 'true';
}
}
It's all untested and sure needs some adjustment for your specific project, but I think you get the idea and I hope it helps.
Next time, please post your schema if you ask stuff about database.
I thought I would improve on this a little bit by wrapping the delete post in a class and function. I was having the same problem. and this worked great for me. Thanks again # Quasdunk
<?php
// Class to hold the remove post function
class someClass{
//Function for removing the post
function removePost(){
if(isset($_POST['delete_id']) && (!empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM post WHERE post_id='".$delete_id."' AND post_member='" . $_SESSION['SESS_USER'] . "'");
if($result !== false) {
echo 'true';
}
}
}
}
if(isset($_SESSION['SESS_MEMBER_ID'])){
$member = $_SESSION['SESS_USER'];
$res = mysql_query("SELECT * FROM post WHERE post_member='$member' ORDER BY timestamp DESC") or die(mysql_error());
$i = new someClass;
while($row = mysql_fetch_array($res)){
echo '<div style="width:100%;margin:0 auto;border-top:thin solid #000;">';
echo '<div style="width:600px;margin:0 auto;padding:20px;">';
echo $row['post_text'] . '<br>';
$postID = $row['post_id'];
echo '<div style="border-top:thin solid #000;padding:10px;margin-top:5px;background-color:#CCC;">';
echo 'You posted this on: ' . $row['post_date'] . '#' . $row['post_time'];
echo '<div style="float:right;">
<form method="post" action="'. $i->removePost() .'">
<input type="hidden" name="delete_id" value="'.$row['post_id'].'" >
<input type="submit" value="Delete Post">
</form>
</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?>
Produce a key to each table, using jquery,then link it to a php file which an accept the key and delete from the specific table (which also can be passed through jquery)

Can each row in an HTML table be numbered?

What I would like to do is number each row in my table. I can't manually number the table myself, as all of the info in it is retrieved from a database. Is this possible with jQuery or PHP? Here's a screen shot of the table:
I tried searching for this, and did not find anything that helped me.
Here is the PHP / HTML that is displaying the table:
<tr>
<th>Name</th>
<th>Email</th>
<th>Subject</th>
<th>Created on</th>
<th style="width:65px;">Status</th>
<th>Actions</th>
</tr>
<?php
[...]
//Display the results
while($info = mysql_fetch_assoc($result)){
//data
$name = $info['name'];
$email = $info['email'];
$subject = $info['subject'];
$ticketid = $info['ticket'];
$isActive = $info['is_active'];
$created = $info['created'];
//status
if($isActive == "1") {
$status = "<span class=\"open\">Open</span>";
$status2 = "active";
}
else if($isActive == "0") {
$status = "<span class=\"closed\">Closed</span>";
$status2 = "closed";
}
else {
$status = "";
}
echo "
<tr>
<td style=\"min-width: 87px;\">$name</td>
<td style=\"min-width:248px;\" title=\"$email\">".addEllipsis($email, 33)."</td>
<td title=\"$subject\">".addEllipsis($subject, 18)."</td>
<td style=\"width: 235px;\">$created</td>
<td title=\"This ticket is $status2\">$status</td>
<td><a href='/employee/employee.php?ticket=$ticketid'>View Ticket »</a></td>
</tr>
";
}
As you can see, it's displayed with a while loop.
If anyone knows a way to number each line in my table with jQuery or PHP, please help me :)
$trCounter=0;
while($info = mysql_fetch_assoc($result)){
//data
$name = $info['name'];
$email = $info['email'];
$subject = $info['subject'];
$ticketid = $info['ticket'];
$isActive = $info['is_active'];
$created = $info['created'];
//status
if($isActive == "1") {
$status = "<span class=\"open\">Open</span>";
$status2 = "active";
}
else if($isActive == "0") {
$status = "<span class=\"closed\">Closed</span>";
$status2 = "closed";
}
else {
$status = "";
}
echo "
<tr>
<td>$trCounter++</td>
<td style=\"min-width: 87px;\">$name</td>
<td style=\"min-width:248px;\" title=\"$email\">".addEllipsis($email, 33)."</td>
<td title=\"$subject\">".addEllipsis($subject, 18)."</td>
<td style=\"width: 235px;\">$created</td>
<td title=\"This ticket is $status2\">$status</td>
<td><a href='/employee/employee.php?ticket=$ticketid'>View Ticket »</a></td>
</tr>
";
}
or you could always use the :eq api in your jquery selector or its equivalent to work with the index but like I asked it depends on what you want to do with the index.
I would go with PHP solution listed by Atul Gupta.
To add more - you also can to start iteration based on which page you are.
<?php
$i = ($page-1) * $itemsPerPage;
while(....)
{
echo $i;
$i++;
}
?>
if you are on the second page of your list would get something like 11,12,13,14,....
jQuery:
$(function () {
var i = 0;
$('table thead tr').prepend('<th>#</th>');
$('table tbody tr').each(function () {
i += 1;
$(this).prepend('<td>' + i + '</td>');
});
});
PHP
<tr>
<th>Sr. No</th> // Add header for counter
<th>Name</th>
...
</tr>
...
$i=1; // add counter -----------corrected-------------
while($info = mysql_fetch_assoc($result)){
//data
...
echo "
<tr>
<td>$i</td> // include counter to table
<td style=\"min-width: 87px;\">$name</td>
...
</tr>
";
$i++; // increment counter
}
Set an auto increment property in the SQL table, which can be used as an index, and will increase automatically when a new entry is added?

Categories