I want to get the value of id so that I can delete the data from mysql based on the id number
This is a project for events, the main idea here is I want to get the id number of the event based on the clicked button, so that I can update/delete the event based on the id number.
Code for displaying details
<?php $sql= "SELECT event_name, event_date, event_id FROM events WHERE event_status=0";
$result= mysqli_query($conn, $sql);?>
while($row = mysqli_fetch_assoc($result))
{
echo '
<div class="pending-card">
<div class="pending-image">
</div>';
echo " <div class='pending-title'>
<h1>{$row["event_name"]}</h1>
</div>";
echo " <div class='pending-des'>
<p>{$row["event_date"]}</p>
<button class='choice-pending'><a href='detail.php'>Read More...</a></button>
<input style='display: none;' type='text' id='test-pend' value='{$row["event_id"]}'>
</div>
</div>
";
}
Jquery code
Here I tried to check if this works by making an alert, but after i press the button, the id number that came out is not correspond with the button i click
$(document).ready(function(){
$('.choice-pending').click(function(){
alert("Value: " + $('#test-pend').val());
});
});
Can anyone tell me where did I go wrong
Example, the event i pressed is suppose to be 34, but the alert shows 26 which is the first event id in the code for displaying details
You can use data-* attribute in your element.
Based on https://www.w3schools.com/tags/att_global_data.asp
The data-* attributes is used to store custom data private to the page
or application.
The data-* attributes gives us the ability to embed custom data
attributes on all HTML elements.
The stored (custom) data can then be used in the page's JavaScript to
create a more engaging user experience (without any Ajax calls or
server-side database queries).
The data-* attributes consist of two parts:
The attribute name should not contain any uppercase letters, and must
be at least one character long after the prefix "data-" The attribute
value can be any string Note: Custom attributes prefixed with "data-"
will be completely ignored by the user agent.
<button class="choice-pending" data-event-id="<?= $row['event_id']; ?>">Read More...</button>
Then in your script you can access the clicked button:
$(".choice-pending").click(function() {
if($(this).attr('data-event-id') !== undefined) {
// You can do ajax call here to your detail.php
// Or you can simply create a hidden field inside your form, assigned the data-event-id value to it, then $("form").submit();
} else {
/** Error message here, maybe? */
}
});
First remove your anchor tag inside button and use only anchor or button and print your html with assign some dynamic class or id into each to make them unique like this.
<div class='pending-des'>
<p>{$row["event_date"]}</p>
<a class='choice-pending-{$row["event_id"]}' href='detail.php'>Read More...</a>
<input style='display: none;' type='text' id='test-pend-{$row["event_id"]}' value='{$row["event_id"]}'>
</div>
Now bind click event on it-
$(document).ready(function(){
$('.pending-des').each(function(){
$(this).on('click', 'a[class^=choice-pending-]', function(){
alert($(this).find('input[id^=test-pend-]').val());
});
});
});
Related
I have this situation.
I've created a table with inline-editing (using jQ), and every row have its edit button. So if I want for instance, to edit first row, I click on edit button and it opens me his input field. Every opened row for editing has its save button which id is connected with id of that property in row.
Now, how can I get to that input value using php? I have to mention every input is with same attribute-name -> they are all creating dynamically.
php
$new_tname = $_POST['edit_team']; ?????????????????????????????????
if (isset($_GET['save_tid'])) {
$tid = $_GET['save_tid'];
$team = Team::getById($tid);
if ($tid == $team->team_id) {
$team->name = $new_tname;
$team->save();
}
}
html/php
<?php
$teams = Team::getAll();
foreach ($teams as $team) {
$tname = $team->name;
$tid = $team->team_id;
echo "<tr>
<td>
<a href='#' class='editable' style='margin-left: 2px;'>".$tname."</a><form method='POST' action=''><input type='text' class='editshow form-control col-sm-3' aria-label='Sizing example input' aria-describedby='inputGroup-sizing-sm' name='edit_teams' value='".$tname."'></form><a href='teams.php?save_tid={$tid}' style='margin-left: 2px; margin-top:3px;' class='btn btn-success btn-sm editshow'>Save</a>
</td>";
echo "<td>
<button class='btn btn-primary btn-sm btnEdit'".$tid."'>Edit</button> | <a href='teams.php?delete_tid={$tid}' class='btn btn-danger btn-sm'>Delete</a>
</td>
</tr>";
}
var_dump($new_tname) throws me a NOTICE: Undefined variable: edit_team
I'm assuming php can't find which edit_team(because they are multiple), isset($_POST['edit_team']) is not solution on this because it throws me NULL,
and here is jQ for inline-editing
jQ
<script type="text/javascript">
$(".editshow").hide();
$(".btnEdit").click(function(){
let btnEdit = $(this),
containerEl = btnEdit.closest("tr");
containerEl.find(".editshow").toggle();
});
</script>
Is there any solution with php or? I hope someone can help me. Thank you.
So the issue is this: when you click on those links (the save or delete), you don't actually tell the server what it is you are updating. The link has no means of knowing, in and of itself, what fields it is sending, or whether they have been updated, or anything. You are doing something dynamic on the page, and in this case, that will mean scripting.
It is an order of magnitude more complex than what you have done with the toggle thing, so be warned -- you have just jumped in at the deep end. I have started building something that will show you how it could be done, but this is by no means complete, or accurate. We don't know what your php is really doing by the snapshot you have given us.
Here is a code block I've built, simply as a prototype. I'll describe it in deeper detail below:
$(function(){
let updateTeamUrl = "SET_THIS_TO_THE_UPDATE_URL";
// We'll also save these as references to all the action buttons.
let actionItems = {
editBtn: $(".btnEdit"),
saveBtn: $(".save-btn"),
deleteBtn: $(".btnDelete")
}
$(".editshow").hide();
// Here we assign the actions to the various buttons/anchors
actionItems.editBtn.on("click", toggleEdit);
actionItems.saveBtn.on("click", saveTeam);
actionItems.deleteBtn.on("click", confirmDelete)
// Below are the actions we assign above, or support functions for them
/**
* toggleEdit() is triggered when the "Edit" button is clicked. This
* hides the team name text element, and displays the editable field.
**/
function toggleEdit(event){
let teamContainer = $(this).closest("tr");
teamContainer.find(".editshow").toggle();
teamContainer.find(".editable").toggle();
}
/**
* saveTeam() crawls up the DOM tree to the closest "tr" node, and gets
* the custom "data-teamid" attribute there. Using that, we can create
* a JSON object that we will send to the backend.
**/
function saveTeam(event){
event.preventDefault();
let clickedEl = $(this),
teamContainer = clickedEl.closest("tr"),
teamNameEl = teamContainer.find(".editshow");
let payloadToSend = {
// We need a mechanism to identify what team we're updating
teamId: teamContainer.data("teamid"),
// This will eventually hold any and all updates.
updates: {
/* We'll create this empty, and fill it as we need.*/
}
};
// I created a data attribute on the input, "data-original", and
// set it to the same value as the input. By comparing these, we
// can see if an input has, in fact, been updated.
if(teamNameEl.data("original") !== teamNameEl.val() ){
/**
* The only thing we are updating in this pass is the team name.
* So we can update our "payload", then simply send the post request.
**/
payloadToSend.updates.name = teamNameEl.val();
/**
* When we POST, we need a URL and a string we'll send.
* What we'll do is take the payload, and run it through
* JSON.stringify() to convert it to a string.
* Then, when we call $.post(), we can also add a .done() and
* .fail() handler. These will run when the save is complete,
* or when it fails.
*
**/
let postRequest = $.post(UpdateTeamUrl, JSON.stringify(payloadToSend) )
.done(function(returnedData){
console.log("Update succeeded: "+returnedData)
})
.fail(function(error){
console.error("Update had an error: "+error)
})
.always(function(){
teamContainer.find(".editshow").toggle();
teamContainer.find("editable").toggle();
});
}
} // end saveTeam();
function confirmDelete(event){
event.preventDefault();
let clickedEl = $(this),
teamContainer = clickedEl.closest("tr"),
teamId = teamContainer.data("teamid"),
teamName = teamContainer.find("[name='edit_teams']").val();
if(confirm("are you sure you want to delete the team "+teamId+": "+teamName+"?") ){
console.log("Deleting!")
} else {
console.log("false alarm.")
}
}
})
Also, the HTML to support this needs to change a little: the <tr> gets a data attribute data-teamid, the <input> gets a data attribute data-original, and the form element that wrapped the input has been removed. Here's the HTML (without the PHP to construct it):
<tr data-teamid="1">
<td>
<a href='#' class='editable' style='margin-left: 2px;'>Team Name</a>: <input type='text' class='editshow form-control col-sm-3' aria-label='Sizing example input' aria-describedby='inputGroup-sizing-sm' name='edit_teams' data-original='Team Name' value='Team Name'> <a href='teams.php?save_tid={$tid}' style='margin-left: 2px; margin-top:3px;' class='btn btn-success btn-sm editshow save-btn'>Save</a>
</td>
<td>
<button class='btn btn-primary btn-sm btnEdit team-1'>Edit</button> | <a href='#' class='btn btn-danger btn-sm btnDelete'>Delete</a>
</td>
</tr>
As I've said, this is getting pretty complex.
First, I moved all your "actions" into a javascript object. If I add other actions, I simply update them there. Then I attach listeners using .on(...) for each action I want to handle: edit, save and delete. For each of these, I wrote a function that will handle the action itself: toggleEdit(...) for the edit, saveTeam(...) for the save, and confirmDelete for the delete. Each action is self-contained, making editing a little easier.
The toggleEdit(...) is almost exactly what you had before. I don't think I changed much in that one, so it should look pretty familiar.
The saveTeam(...) is the one you're asking about now, and it is the complex bit. First, we climb back up the DOM tree to the <tr>, and use .data("teamid") to get the id of the team we're editing. Then we start creating a javascript Object, which I've called payloadToSend. In this, we have an attribute teamId, and a nested object updates. Next, we check to see if the text input has been updated (by comparing its value with that data-original attribute I added). If it hasn't changed, we don't need to update. If it has, then we update the payloadToSend by adding name to the payloadToSend.updates object, and we use $.post(...) to actually send the POST request. $.post() takes a URL and some data. We'll send the payloadToSend object as that data, which should show up in php as either $_REQUEST or $_POST. In addition, the $.post(...) includes two conditional callbacks, .done(...) and .fail(...), and one "absolute" callback, .always(...)
.done(...) runs when the server-side request has returned with a successful complete, and .fail(...) handles a back-end failure with an error message. In either case, though, we .always(...) want to hide the editable and show the team name again.
At this point, the confirmDelete action doesn't do anything more than get that same teamId, and pop up a basic confirmation dialog. The delete action is quite a bit more than that, and beyond the scope of your original question.
You will need some additional processing and filtering for the security of the data but a simple solution is to change the name of the field to edit_teams_{$tid} and in your php loop through the passed form fields:
<?php
foreach($_POST as $field_name => $field_value){
if(substr($field_name,"edit_teams") !== false){
$edit_teams = $field_value;
$team_id = str_replace("edit_teams_","",$field_name);
}
}
?>
I have a small search engine querying the database for a list of names. The engine returns a maximum of 5 names. There is a button next to each each person's name. When the button is clicked, my jQuery code is supposed to add only that person's name to a comma separated list. Currently, it is adding every name that the engine pulls up. Im assuming I need to utilize the this command somehow. I feels as if my div isn't properly being selected.
The Question: how do you access the text of a paragraph that exists in the same class as the button?
The paragraph and button are enclosed by a class. The paragraph has a class. The button has a class.
//jQuery function to add the name to my list of names
$(document).ready(function(){
var addList = $('.addList');
$(".addCustomer").on( "click", function() {
customerToAdd = $('.addIndividual > .searched_name').text(); //here is where the problem lies. it comma separates every name
addList.val($('.addList').val() + customerToAdd + ', ');
search.val('');
});
});
And here is my html enclosed in php. This holds the fields that are used by the jQuery above.
while($state = mysqli_fetch_assoc($states)) {
$customerid = $state['id'];
$customername = $state['name'];
echo "
<div class='addIndividual' >
<p style='float:left;' class='searched_name'>".$customername."
</p>
<button type='button' value=".$customername." class='btn btn-default addCustomer'>Assign List to a Class</button>
</div>
<hr>";
}
You need to change
$('.addIndividual > .searched_name').text();
to
$(this).val();
OR if not the same as in the customer paragraph (it does seem that it is now):
$(this).closest(".addIndividual").find('.searched_name').text();
or if the paragraph stays next to the button for sure:
$(this).prev().text();
It looks like you already have the customer's name in the value attribute of the button. You can just grab it with: $(this).val().
You should also change your button to use class="addCustomer" instead of id="addCustomer". ID's are for unique elements, while a class is for multiple elements. In your case, you have a button for each customer.
Instead of $(".addCustomer").on( ... create a function like this:
function addCustomer(index){
$('.addIndividual .searched_name_'+index).text();
// continue the script ...
}
Your while loop will now look like this:
<p style='float:left;'class='searched_name_".$customerid."'>".$customername."</p>
I have a view of teasers in Drupal. Each teaser has a click() handler which is supposed to send its node id as an argument to load a view via ajax. I've tried 2 different jquery approaches, but am having no luck.
The first example sends the nid for only the last node in the view. So that no matter what teaser I click only the nid for the last teaser gets sent.
Drupal.behaviors.ajaxview = function(context) {
$(".ajaxclick").click(function(){
$(".container").load(Drupal.settings.basePath + "myajax/" + <?php echo $node->nid;?>;);
});
}
In the second approach, a click on a button of class "ajaxview" will send the correct nid but but instead of sending for just the one clicked button to it's corresponding div, it will send a nid for EACH button with a class of "ajaxview" into EACH div with a class of "container". So I end up with the contents of every single view generated from every single teaser into every single div. WAAAAY too much!
$(document).ready(function() {
$(".ajaxclick").click(function(){
$(".container").load(Drupal.settings.basePath + "myajax/" + <?php echo $node->nid; ?>);
});
});
Here is the button;
<button class="ajaxclick">click</button>
And the div:
<div class="container"></div>
Any idea how I can get each click to send the nid of that teaser clicked upon as the argument and load only that view?
Your code is javascript.
You can't use:
<?php echo $node->nid; ?>
You will have to use jQuery approach to get the nid you want.
For example, if you have the nid in a hidden filed like:
<input type="hidden" id="ID" value="100" />
You can use the following jQuery to get the nid:
var nid = $("#ID").val();
Turns out I was able to get it working using a revision of Ionut.A's suggestion;
Drupal.behaviors.ajaxview = function(context) {
$(".ajaxclick").click(function(){
var nid = $(".mynid").eq($('.ajaxclick').index( $(this) )).val();
$('.container').eq($('.ajaxclick').index( $(this) )).load(Drupal.settings.basePath + 'myajax/' + nid);
return false;
});
}
and for the html;
<input type="hidden" class="mynid" value=<?php echo $node->nid;?> />
I had to use class instead of id as the id would only return the id from the first node.
Using some code to create a form dynamically which I got here: http://www.trans4mind.com/personal_development/JavaScript2/createSelectDynamically.htm
This works great. However I have a regular html table I generate with html/php to get data out of a DB. I want to replace that data with a form so when users click the edit button the original entry is replaced with a form (either textbox or pull down menu). The user makes a selection and the new table comes back with the appropriate edit.
So for example one part of the data has this in the table:
<td><?php echo $result[0] ?></td>
Using the link about to create a form dynamically I change this to:
<td id="paraID"><form id="form1" name="form1" method="post" action enctype="text/plain" alt=""><?php echo $result[0] ?></form></td>
Also note the onclick event for the edit button:
This is hard to explain but hoping someone can help me with this interaction. I need some way to say:
if (user clicks edit button)
then
replace html table with form for each entry (for example, the table returns a name called foo and a textbox will appear with foo in it but now they can edit to change the name).
If you can start out with an id for the td then it will make things easier. Then you will need an edit button somewhere. Notice: It might be nice to replace "result_0" with the name for the value/field:
<td id="result_0_parent"><?php echo $result[0] ?><input type="button" onClick="editField('result_0','select')" value="Edit" /></td>
Then in your javascript you will have the editField function defined so that it sets the content of the td to be the dynamic form. Looking at makeForm function in the example javascript, you see this happening with appendChild(myform); The function editField will be like the makeForm function except you will pass in the field_id and field_type as parameters:
function editField(field_id, field_type)
I suggest you change the line that defines mypara to define mytd or better yet, field_parent instead since in your case it will not be a paragraph element, but a td (or possibly some other type of element):
field_parent = document.getElementById(field_id+"_parent");
The example code create a select (dropdown), but I am guessing you want to create other field input types so I recommended having field_type as a second parameter to the function. This means that it would make more sense for your implementation to use myfield instead of myselect and then use the field_type parameter to decide what myfield will be.
Replace the line in the makeForm / editField function:
myselect.setAttribute("id","selectID");
with
myfield.setAttribute("id",field_id);
One more thing: To set the initial value of the input field to be the displayed content, you will need to copy the "innerHTML" of the "parent" element. So place something like this right after defining field_parent:
initial_value = field_parent.innerHTML;
and I think you can figure out the rest. If not, I can elaborate a little more.
This works great. However I have a regular html table I generate with
html/php to get data out of a DB. I want to replace that data with a
form so when users click the edit button the original entry is
replaced with a form (either textbox or pull down menu). The user
makes a selection and the new table comes back with the appropriate
edit.
This is a script that allows with a double click on values to edit them and has a button to send them back. Maybe it would be of some help to use it (or use parts of it).
<?PHP
if(count($_POST)>0)
{
echo 'You gave:<br><per>';
print_r($_POST);
echo '<a href=http://localhost/temp/run.php>Start over</a>';
exit;
}
?>
<html>
<head>
<script type="text/javascript">
/**formEditor Class
*/
function formEditorCls( )
{
/**
Constructor simulator
*/
this.lastFieldEditedId = null;
/** Change span with input box, hide the eddit button and store theses IDS
*/
this.edit=
function (field)
{
//if there was a field edited previously
if(this.lastFieldEditedId != null)
this.save();
//get the inner element of the div, it can be span or input text
var childElem = document.getElementById(field).getElementsByTagName('*')[0];
//then replace the span element with a input element
document.getElementById(field).innerHTML="<input type=text name=n_"+field+
" id=id_"+field+" value="+childElem.innerText+">";
//store what was the last field edited
this.lastFieldEditedId =field;
}//func
this.save=
function ()
{
dbq="\"";sq='\'';
//get the last value
var lastValue = document.getElementById(this.lastFieldEditedId).
getElementsByTagName('*')[0].value;
//store it as span
document.getElementById(this.lastFieldEditedId).innerHTML="<span ondblclick="+dbq+
"formEditor.edit("+sq+this.lastFieldEditedId+sq+");"+dbq+" >"+lastValue+"</span>" ;
//now must reset the class field attribute
this.lastFieldEditedId=null;
}//func
this.submit=
function (path)
{
this.save();//if ay field was edited put new values in span elements
var form = document.createElement("form");//create a new form
form.setAttribute("method", "post");
form.setAttribute("action", path);
var myDiv = document.getElementById( "fieldsDiv" );//get the div that contains the fields
var inputArr = myDiv.getElementsByTagName( "SPAN" );//get all span elements in an array
//for each span element
for (var i = 0; i < inputArr.length; i++)
{
var hiddenField = document.createElement("input");//create an input elemet
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", i);
hiddenField.setAttribute("value", inputArr[i].innerText);
form.appendChild(hiddenField);//append the input element
}
document.body.appendChild(form);//append the form
form.submit();//submit the form
}//func
}//class
formEditor = new formEditorCls( );
</script>
</head>
<body onclick="rt();">
Double click any value to change it..<br><br>
<div id="fieldsDiv">
Name:<font id="nameField">
<span ondblclick="formEditor.edit('nameField');" >Mark</span>
</font><br>
Surname:<font id="surnameField" >
<span ondblclick="formEditor.edit('surnameField');">Smith</span>
</font><br>
</div>
<input type=submit name="submit"
onclick="formEditor.submit('http://localhost/temp/run.php');" value="Submit">
</body>
</html>
I have two forms
<form name='myform1'>
<input type='button' class='submitBtn'>
</form>
<form name='myform2'>
<input type='button' class='submitBtn'>
</form>
Both forms have a button with same class. How can I capture click event of only myform1's button(submitBtn) in jQuery?
Thanks.
WRONG: u cant have same id on a same page
otherwise do below
TRY this
$('input[type="button"]').click(function() {
var formName=$(this).parent('form').attr('name');
if(formName =='myform1'){
alert( formName+' button is clicked');
} if(formName =='myform2'){
alert( formName+' button is clicked');
}
});
DEMO
It's not allowed to have more than one id with the same value.
Solution: use class names for visual styling and distinct IDs for capturing events.
id means identifier -- so, it should be unique in the HTML page, to allow one to identify an element.
Still, if you want to get the button which is in the form named myform1, you could use something like this :
$('form[name="myform1"] input[type="button"]')