Running php from list element - php

I have a list on one of my website pages that acts like a delete button, however I was wondering if you could call a php script when the user clicks on it, a bit like a submit button.
Here is the list:
<li>
<a class="actions_dropdown" href="#"> </a>
<ul class="actions_content">
<li>Delete</li>
<li>Report</li>
<li>Mark as Read</li>
<li>Mark as Unread</li>
<li>Move to Folder 1</li>
<!----whole loop of folders--->
</ul>
</li>
Is there anyway so when the user clicks on, say the delete li that it calls a php script? I can't seem to work it out.

assign a class to your Delete li like
<li><a class="actions_dropdown" href="#"> </a>
<ul class="actions_content">
<li class="del">Delete</li>
<li>Report</li>
<li>Mark as Read</li>
<li>Mark as Unread</li>
<li>Move to Folder 1</li>
<!----whole loop of folders--->
</ul>
</li>
now attach a click event handler to the li with class del
$("li.del").click(function(e){
var db = $(':checkbox:checked').map(function(i,n) {
return $(n).val();
}).get(); //get converts it to an array
if(db.length == 0) {
db = "none";
}
$.post('/test.php',{'db[]':db},function(data){
console.log(data);//it should log data deleted successfully
});
});
in test.php
<?php
//delete logic here
echo json_encode("data deleted successfully");
?>
Post array of multiple checkbox values

You can simply set the anchor's href to your PHP page, but as with a standard form submission that would either navigate away from the current page or cause the current page to reload (depending on what your PHP returned).
So probably you want to make an Ajax request. Unlike standard form submission, Ajax doesn't cause the current page to reload, it returns the response back to your JS code.
Assuming you can add an id to your li or anchor element (e.g., id="deleteLink"), or otherwise identify it, you can bind a click handler that does the Ajax:
$("#deleteLink").click(function(e) {
e.preventDefault();
$.ajax({
url: "yourPHPpageHere.php",
data: { paramName1 : paramValue, paramName2 : paramValue2 },
dataType : "json" // or "html", "xml", or "text", whatever your PHP returns
}).done(function( response ) {
// optionally do something with response.
});
});
Any data you need to pass to the delete PHP (presumably some kind of record id at minimum) can be set in the data parameter of the $.ajax() call.
For more detail about the $.ajax() method see the jQuery doco.

Related

php submit one of clicked <li>

To display data I have to use onClick on the li tag otherwise I could pass data,
I need to submit clicked item from list, noted that this list is created by loop that mean this name is an array.
How can I submit(post) the value of a clicked (li:input)? I mean the current list index which I clicked?
<ul>
<?php
for($i=0;$i<5;$i++){?>
<li>.....value....</li>
<input type="hidden" name="hide[]" value="..value.."/>
<?php }?>
</ul>
I tried before foreach but I cant get my proper own value that I need.
This is only an example of onclick. You have to modify it as per your requirement.
$(document).ready(function(){
$("#class").click(function(){
var name = $(this).val();
$.ajax({
type:"POST",
data:{"name":name},
url:"page_name.php",
success:function(data){
$(".success").html(data);//Here you can show success data
}
});
});
});

How to display appending data without refresh in tree

This is my tree view
I have already done that when i do right click tree name(For example ,Asset, Non Current , Shah) and click add head Then there will come jquery dialog form and I add head code and head name and it is saved successfully in mysql database using codeigniter frame work in php.
Basically , it is created subhead under head.
I need when after submitting, it will be display subhead under that head without refresh tree. For Example ,
If i create subhead under asset then append subhead after "104-Kamrul" without refresh and Display without Change.
How can i solve it, Please any suggestions?
i think you need ajax to make what you need, just use a specific class for each tree parent for example:
<div class="parent-1">
<div class="child-1"></div>
<div class="child-2"></div>
</div>
<div class="parent-2">
<div class="child-1"></div>
<div class="child-2"></div>
</div>
now just load what you need following parents classess:
$('.parent-1').children('.child-1').load('http://site.com/url/to/load/new/childs');
or
var _childs = $.get('http://site.com/url/to/get/some/childs');
$('.parent-1').append(_childs);
Given the following markup:
<div id="tree">
<ul>
<li>Asset
<ul>
<li>101-Non Current</li>
<li>102-Current Asset</li>
<li>103-Abdul Halim Shah
<ul>
<li>10301-Shah
<ul>
<li>1030101</li>
</ul>
</li>
</ul>
</li>
<li>104-Kamrul</li>
</ul>
</li>
<li>2-Expenses</li>
</ul>
</div>
I wrote a couple of jQuery functions to dynamically add new nodes. One adds the node below the selector specified and the other adds it as a sibling:
function addSibling(selector, content) {
var markup = '<li>' + content + '</li>';
$(selector).parent().append(markup);
}
function addChild(selector, content){
var obj = $(selector);
var markup='<li>' + content + '</li>';
var element;
if ($('ul', selector).length > 0){
//nested list
element = obj.find('> ul');
} else {
element = obj;
markup = '<ul>' + markup + '</ul>';
}
element.append(markup);
}
Although you'll have to adapt them to your code, you can use the following click function to test them:
$("#tree ul").click(function (e) {
//addSibling(e.target, 'new one');
addChild(e.target, 'new one');
e.stopPropagation();
});
I didn't understand what your variable were, but to get the data to the server without a page load, you can use an ajax function something like the following:
$('#form').submit(function() {
var headCode = $('input[name="headCode"]').val();
var headName = $('input[name="headName"]').val();
$.ajax({
url: "load.php",
type: 'POST',
data: {
head_code: headCode,
head_name: headName
},
cache: false,
success: function(json){
alert('loaded');
}
});
return false;
});

Loading Javascript into AJAX generated div

I'm trying to load Javascript into a div that has been generated through AJAX and filled with an external PHP file.
Here is the navigation I am using to load the AJAX div, and my PHP content:
<div id="portfolioContent">
<div id="portfoliotabContainer">
<ul id="portfolioTabs">
<li><a id="dashboardTab" href="./portfolio/portfoliocontent/dashboard.php">Dashboard</a></li>
<li><a id="casedetailsTab" href="./portfolio/portfoliocontent/casedetails.php">Case Details</a></li>
<li><a id="correspondenceTab" href="./portfolio/portfoliocontent/correspondence.php">Correspondence</a></li>
<li><a id="pleadingTab" href="./portfolio/portfoliocontent/pleading.php">Pleading</a></li>
<li><a id="discoveryTab" href="./portfolio/portfoliocontent/discovery.php">Discovery</a></li>
<li><a id="expensesTab" href="./portfolio/portfoliocontent/expenses.php">Expenses</a></li>
<li><a id="indexTab" href="./portfolio/portfoliocontent/docindex.php">Document Index</a></li>
</ul>
</div>
I need the hyperlink to load and fill the generated div, but also call an external Javascript file so my scripts work within the generated div.
Here is my script:
$(function () {
$("#portfolioTabs li a").live("click", function () {
var pagecontent = $(document.createElement("div"));
pagecontent.load(
$(this).attr("href"));
$(".insideContent").html(pagecontent);
$(".portfolioContent").animate({
height: "110%"
});
$(".insideContent").animate({
bottom: "0px"
});
return false;
});
});
I've looked into this and I understand that I can load is dynamically, but I'm unsure of the simplest way to achieve that.
The problem seems to be that you are assigning the content to another element when it is not loaded yet.
You can use the callback function of load for that:
pagecontent.load($(this).attr("href"), function() {
// this gets executed when the `load` is complete
$(".insideContent").html(pagecontent);
$(".portfolioContent").animate({
height: "110%"
});
$(".insideContent").animate({
bottom: "0px"
});
});
You should use something like $.ajax. This allows you to asynchronously load content into your page without a page reload, which sounds like what you want.
Within your event handler, you would probably want something like
var url = $(this).attr("href");
$.ajax({
url: url,
success: function(data) {
$(".insideContent").html(data);
}
});
Note that everything that depends on the data loading must be placed within the anonymous function where I update the html.

Removing link ID element from jQuery tabbed search script

I have a jQuery tabbed search script that gets content from a PHP file defined by the link and parses it to the results div element. The ID for each link is used to pull content from the correct file however type_ is needed in the link ID for the tabs to work which then doesn't pull content from the right place. How can I resolve this issue?
This is my current jQuery code:
$(document).ready(function(){
$("[id^=type_]").click(function(){
type=$(this).attr("id");
$("[id^=type_]").removeClass("selected");
$("#"+type).addClass("selected");
return false;
});
$("#type_tab1").click();
$("#query").keyup(function(){
var query=$(this).val();
var yt_url=''+type+'.php?q='+query;
if(query==''){
window.location.hash='';
document.title='My Search Script';
}
$.ajax({
type:"GET",
url:yt_url,
dataType:"html",
success:function(results){
$('#results').html(results);
}
});
});
});
This is my HTML code:
<ul>
<li><a id="type_tab1" href="javascript:void(null);">Tab1</a></li>
<li><a id="type_tab2" href="javascript:void(null);">Tab2</a></li>
<li><a id="type_tab3" href="javascript:void(null);">Tab3</a></li>
</ul>
If I've understood your question correctly, you want to use only the part after the underscore character when you send your GET request.
If that's the case, you can do this in your click event hander:
type=$(this).attr("id").replace("type_", "");
Note that you can simplify this, because you don't need to use jQuery to get the id attribute:
type=this.id.replace("type_", "");

Loading tab by default in jQuery tab script

I have a jQuery tab script that gets content from a PHP file defined by the link and parses it to a div element. When a user selects a tab, it turns bold. However, currently when the script is loaded up, a tab is not selected by default. My question is how can I load up a certain tab and it's contents by default and show that the tab is selected with my current code?
This is my current jQuery code:
function load(url){
$.ajax({
url:url,
success:function(message){
$("#content").html(message);
}
});
}
$(document).ready(function(){
$("[id]").click(function(){
type=$(this).attr("id");
url=""+type+".php";
$("[id]").removeClass("selected");
$("#"+type).addClass("selected");
load(url);
return false;
});
});
This is my HTML code:
<ul>
<li><a id="tab1" href="javascript:void(null);">Tab1</a></li>
<li><a id="tab2" href="javascript:void(null);">Tab2</a></li>
<li><a id="tab3" href="javascript:void(null);">Tab3</a></li>
</ul>
Trigger the click event of the tab you want to load:
e.g. -
$(document).ready(function(){
$("[id]").click(function(){
type=$(this).attr("id");
url=""+type+".php";
$("[id]").removeClass("selected");
$("#"+type).addClass("selected");
load(url);
return false;
});
//load default tab
$("#tab1").click();
});

Categories