PHP multiple edit with checkbox - php

I'm trying to make a form which can edit/delete multiple rows in mysql database, for example is phpmy admin tabel editor:
So if i checked several data, and click change/edit, it will only show the checked data editor, like this:
and if i click delete, it will delete the checked data only, and if possible, i want the checked data to show in popup, instead in new page like if i use:
<a href ='view2.php?id=$id'>Edit</a>
I hope anyone can give me some solution or reference for my problem, thanks.

I think you can create new checkbox column and new edit multi button below table.
<input type="checkbox" value="<?php echo $USER_ID; ?>" name="chk[]">
User can be multi select checkbox. When submit button pressed, you get array User ID. array(1,2,3...)
You pass array into sql command, where ID in array above
I think that can be help you.

You can make use of ajax concept it will be useful to populate the data on the popup box which was requested by you.
<a class="popup" href ='view2.php' attr_id='<?php echo $id; ?>'>Edit</a>
Where the attr_id was the user defined attribute.
$('.popup').click(function() {
$.ajax({
url: base_url+'YOUR_URL/'+$(this).attr('attr_id'),
type: 'GET',
success: function(data) {
$('#content_popup').html(data);
}
});
});
Here you can use ajax to get the details. And load it on the #content_popup after that you can trigger to make #content_popup by using some plugins. (Fancybox,colorbox)

Related

To perform ajax functioning with jquery

Simply, I have an <a> tag having values(ID) which will be posted to the same page in clicking,
I wanted to load data into the table based on the ID provided by the post method. On the other hand I am having a clock which is rested again and again when fired a post method.
I simply wanted to do the same via jQuery.
In short I wanted to Implement ajax using jQuery
Also I am using database MySQL, with PHP scripting
Any sort help will be appreciated.
<a class="D" href="?ID=<?php echo $rows[0]; ?>" onclick="">Question<?php echo $QNo; ?></a>
I wanted the same above and get ID for searching relevant data against the ID.
I might be wrong here, but you basically want to query a PHP page based on an ID and display relevant content in a div?
To make an AJAX call, setup a PHP page that will query your database and in turn return HTML data which you can then display in a DOM element.
A very simple example of this would be:
HTML
Load table 1
Load table 2
<div id="contentToPopulate"></div>
jQuery:
$('a.linkToLoadTable').click(function(){
var pageId = $(this).data('tableId')
$.ajax({
url: 'loadTable.php?id='+pageId
}).done(function(data) {
$('#contentToPopulate').html(data)
});
})

Passing PHP variables to jQuery pop-up DIV

I'm quite new to PHP and I'm dealing with a problem I have no idea how to solve. I'm generating a table of users (name, username, e-mail) from a MySQL database. There is an edit link in each row. This links displays a pop-up DIV (made using jQuery) that contains three form fields (name, username, e-mail) that can be used to edit (update) the particular row.
I would like to pre-load the current values from the database into these form fields to make it more convenient to edit them.
The problem is that the DIV is displayed without refreshing the web page, meaning that I'm unable to pass any parameters from the edit link and therefore I cannot tell the server which values to pre-load.
Is there any solution to this?
Thanks!
You can pass them using this way:
Note : when u generate the values(name,username,email) in Server (using PHP)..do it as follows :
<a class='editBtn' data-x="the username info" data-y="the email info" data-z="name">Edit</a>
then at Client side catch them using on click events:
jQuery('.editBtn').each(function(i,v){
jQuery(v).click(function(eve){
eve.preventDefault();
eve.stopPropagation();
var username= jQuery(v).attr('data-x');
var email= jQuery(v).attr('data-y');
var name= jQuery(v).attr('data-z');
/*
display ur Modal Box here
Happy Coding :) ,
*/
});
});
try this:
$(your var in JS) = <?PHP echo $(your rowname in the prepaired statement)['(your ID tablevariable)'];?>
that could work but i hadn't seen the code so, this is the only thing i could give tou to try
if you want to refresh the data in realtime about your html with your new values, you must use AJAX. JQuery give us a simple api about this: https://api.jquery.com/jQuery.ajax/
I like to use a hidden <iframe> at the bottom of all my pages. I can load a php script into that, which than can be used to update the parent frame by using javascript.
<iframe name="hidden" id="hidden" style="display:none"></iframe>
To call the script, just use a link or button to call it.
Click to load script!
Once you get the data from mysql, use javascript to change the variables.
<script language="javascript">
parent.document.getElementById('name').value = '<?PHP= $sqlResults['name']; ?>';
parent.document.getElementById('username').value = '<?PHP= $sqlResults['username']; ?>';
parent.document.getElementById('email').value = '<?PHP= $sqlResults['email']; ?>';
</script>

Delete DB row on back end with <a href></a> on front end

I have a row of links like this:
Delete | Votes (2) | Comments (1)
They are each associated with user posts.
My question is on Delete:
All I want user to do is click it and then this needs to happen on php side:
<?php
$reviewId = $database -> escape_value(trim($_POST['reviewId']));
$user_id = $database -> escape_value(trim($_POST['user_id']));
// Delete Review Method
$result = Data::deleteMyReview($reviewId);
?>
My questions, what is the most efficient way of writing the code for Delete?
I don't really want an entire form for that one word. But then it also seems like a security issue to continue with the Delete approach. Then anyone can delete user comment by altering the url.
So should I use Post vs Get and Form vs <a>?
Using a form with the post method seems more appropriate for a delete action. I would have a separate form for each row, containing a hidden input with the ID and a submit button. A single form would work but you would have to have some Javascript to set the ID when each button is clicked.
A form is no more secure than a link. To make either way secure, you need to verify that the current user is authorized to delete the target review. For example, check that he owns the review. This should be done right before the delete code is executed.
It's fine to hide the delete button for reviews that don't belong to the user, but you should not rely on that for security because anyone can post a form and set the review ID to delete, regardless of what you were hiding and showing on the page.
You can use an <a> element and still POST the data. For example, using jQuery to perform an AJAX post:
HTML:
Delete
jQuery:
$(document).ready(function(){
$('.delete-review').click(function(){
$.ajax({
type: "POST",
url: "thispage.php",
data: { reviewId: $(this).data('review-id'), userId: $(this).data('user-id') },
success: function(data) {
//Do whatever you want to do when the delete succeeds such as redirect to another page
},
error: function(jqXHR,textStatus,errorThrown) {
//Handle your error here
}
});
return false;
});
});
As stated in my comments, for security concerns, you definitely need to validate the data before performing the delete. Don't trust it just because the delete option should only been seen by a valid user and you're using POST. Always validate.

Adding a select input dynamically using CakePHP

When a user adds an event, they need to be able to add Bands from the "bands" table to the event. It's already all set up with the HABTM, and I have it working when I hard-code multiple select boxes to the page.
The problem is - I'd like to just have one select box, then an "add another band" button - which would add another select input with the list of bands - and so on - as many as they'd like.
I found this post: Add and remove form fields in Cakephp which explains how to add a field dynamically... my issue is, the list of bands is huge, and changes regularly, so I can't imagine this working for me.
Any ideas on the best way to go about this? - Adding a select input dynamically that's populated with a list of bands from my database? Ajax maybe? (I've no idea how to do ajax with cake yet) Ajax seems ok, but do I really want to pull a list of bands every time the user clicks the "add a band" button? Maybe that's ok?
Any help/direction is greatly appreciated. Code example would be GREAT, but if nothing else, a nudge in the right direction would be very helpful.
You could use a single select input with an 'add band' button. When the user hits 'add band', catch the event with javascript, copy the selected band to a list (visually), and add the id to a hidden input (to be used when the form is submitted). jQuery/CakePHP example below.
<ul id='band_list'></ul>
<?php echo $form->create('Event', array('id'=>'event_form'));?>
<?php echo $form->input('band_ids', array('type'=>'hidden', 'id'=>'band_ids')); ?>
<?php echo $form->input('bands', array('type'=>'select', 'options'=>$bands, 'id'=>'bands_selector')); ?>
<button id='add_band'>Add Band</button>
<script type='text/javascript'>
var band_count = 0;
$('#add_band').click(function(event) {
event.preventDefault();
$('<li>' + $('#bands_selector option:selected').text() + '</li>').appendTo('#band_list');
$('<input type="hidden" name="data[Band][Band]['+band_count.toString()+']" value="'+$("#bands_selector option:selected").val()+'">').appendTo('#event_form');
band_count++;
});
</script>

Changing an checkbox input name with a button click function

I'm running into a problem with changing the name attribute on an input element when a button is clicked.
I have 3 buttons:
* Add Renter
* Delete Customer
* Update Customer
This is the input element:
<input type=\"checkbox\" name=\"todelete[]\" value=\"$busRow[0]\" class=\"check\" />
$busRow is the user's id..
The form is submitted to a php script that checks to see what button was set and does different actions for that button. When the user clicks the delete it loops through and deletes each customer that was in the todelete[] array. When the user clicks the Update Customer button i want to change the checkbox input's name to "id". Then my php script will grab the user's id instead of grabbing the array value and then redirect the user to the Update Customer page with the id stored on the URL.
This is the javascript i'm using at the bottom of the page:
<script type="text/javascript">
$("button").click(function () {
if ($(this).text() == "Update Customer") {
$('input.check').attr("name", "id");
}
});
</script>
When i get to the Update Customer page the id is not there in the URL and i can't pull any values based upon the ID :(
You're setting the name value to "id", instead of the value stored in value. Try this:
$("button").click(function () {
if ($(this).text() == "Update Customer") {
var element = $('input.check');
element.attr("name", element.attr("value"));
}
});
Although, I think you could just simply pass the value of the input tag instead of the name when the btn clicked is 'Update'. That would save you the hassle of changing these different attributes and could save you trouble.
Edit: I'm reading the OP again and see you want to set the name att to "id" and not the user's id??? You're question is vague and hard to understand what you're trying to do. You're leaving out crucial code that grabs this "id" and redirects the user.
Edit2: What's up with the semi-colon between your string concat? I hope that's not in your source code. Now seeing your redirect code. I think your problem lies in trying to change the tag's attributes/values with an 'input.check' selector. I would also say you should redesign the way you are implementing this. Your checkboxes shouldn't hold your user information. But regardless, I don't think you need to even change the name value. If update isset() then just make $id = (its current name value); Download firebug to debug your javascript code. Firebug will also show you errors in your code that wouldn't see otherwise. It should help you out a lot
Try putting an alert inside the if statement to check if that is actually becoming true, to narrow down what might be the cause.
Also I've never seen input.check used as a selector. Perhaps give the button an id making it:
<input id=\"customercheck\" type=\"checkbox\" name=\"todelete[]\" value=\"$busRow[0]\" class=\"check\" />
Then change the attribute via the ID like so:
$('#customercheck').attr("name", "id");
that also saves you if you add another input to the field later down the road.

Categories