I am trying to write a simple Javascript snippet into a Codeigniter link. I am using the link to delete posts where required in my dashboard. I dont know anything about JS although am trying to learn it.
Code
$js = 'onClick = "alert("Are you sure")"';
$this->table->set_heading('Date', 'Title', 'Delete', 'Update');
foreach($records as $row){
$row->title = ucwords($row->title);
$this->table->add_row($row->date,
$row->title = ucwords($row->title),
anchor("main/delete/$row->id", $row->id, $js), //this is the link in question
anchor("main/fill_form/$row->id", $row->id)
);
}
$table = $this->table->generate();
echo $table;
My question is how to write the JS for the link ($js). I would like to use a confirm statement, (yes or no). I am totally lost with JS to prevent accidental deletions
Thank you
Here's how you might do it with the CodeIgniter anchor function :
echo anchor('delete/something', 'Delete', array('onClick' => "return confirm('Are you sure you want to delete?')"));
This displays a confirmation box when the link is clicked. If the user confirms then the link is followed. If the user cancels then no action is taken.
Related
I have created a front end page on my Wordpress site where I can submit data through a form, and it will display all the data in a table on the same page. I would like to have a delete option for each row but I am not sure which way to go.
The code I am using to display the database information works fine.
/* Displays Table and Data*/
foreach($cogs as $cogs){
echo "<tr>";
echo "<td>".$cogs->id."</td>";
echo "<td>".$cogs->date."</td>";
echo "<td>".$cogs->source."</td>";
echo "<td>".$cogs->items_purchased."</td>";
echo "<td>".$cogs->receipt_total."</td>";
echo "<td>" "</td>";
echo "</tr>";
}
In the line after "receipt_total," what code should I write? Should I include an anchor tag and link the delete button to another page? I have tried an HTML link, and a HTML button. My problem is making the button or link delete the row from the database. Someone suggested this code, but I am not sure I am using it right. My gut instinct is to place this code in my functions.php file, but I am not sure.
<?php
if(isset($_GET['id']) &&!empty($_GET['id'])){
$id = $_GET['id'];
$table = 'cogs';
$wpdb->delete( $table, array( 'id' => $id ) );
wp_redirect('URL');
}
?>
I am trying to display an icon image and give that image a link with the intern details but not working. I am trying to do like within a cakephp code i am trying to show an image and when a user will click on that image it will show another page with these array('action' => 'detail'), $intern['Intern']['id']) details.Here is my code below. What's wrong with these code
<?php
echo $this->Html->link(($this->Html>image('.img/resource/hover_down_icon.png')),array('action' => 'detail'), $intern['Intern']['id']),array('css' =>'image_down_icon');
?>
Trying to display an image and give it a link,Use this type of method
echo '<img src="hover_down_icon.png" />';
OR
echo "<img src=\"hover_down_icon.png\" /> ";
if you want to send details to another page set the details in a variable and send ,then retrive value at next page by
//Using GET, POST or COOKIE.
$var_value = $_REQUEST['details'];
Try to alter your code with adequate changes
you are using wrong perameter in the action array .you need to use array('action' => 'detail', $intern['Intern']['id']) as one parameter. Try this one
<?php
echo $this->Html->link(($this->Html>image('.img/resource/hover_down_icon.png')),array('action' => 'detail', $intern['Intern']['id']),array('css' =>'image_down_icon'));
?>
I am making a website in CodeIgniter and for one of these pages I need to insert information into a database, however every time I enter information into my form and submit it, the page refreshes like it had been submitted but nothing enters the database.
Controller:
public function insertjob()
{
$this->load->helper('form');
$data['title']="Add a new job";
$this->load->view("insertjob", $data);
}
public function addingjob()
{
$jobtype=$this->input->post('jobtype');
$jobinfo=$this->input->post('jobinfo');
$this->load->model("cmodel");
if($this->cmodel->addjob($jobtype, $jobinfo)){
$data['msg']="New job addition successful";
}else{
$data['msg']="There was an error please try again";
}
$this->load->view("confirmation",$data);
Model:
function addjob($jobtype,$jobinfo)
{
$newjob=array("jobtype"=>$jobtype,"jobinfo"=>$jobinfo);
return $this->db->insert('clientjobs', $newjob); exit;
View:
</p>
<?php
echo form_open('client/insertjob');
echo form_label('Job:', 'Job');
echo form_input('jobtype');
echo form_label('Job information:', 'Job information');
echo form_input('jobinfo');
echo form_submit('Add job', 'Submit Post!');
echo form_close();
?>
Try removing the exit from your model:
function addjob($jobtype,$jobinfo)
{
$newjob=array("jobtype"=>$jobtype,"jobinfo"=>$jobinfo);
return $this->db->insert('clientjobs', $newjob);
}
It's not neccessary and could be breaking the database class, as well as halting any execution for the application.
Here's your problem:
echo form_open('client/insertjob');
If you look at your HTML code in your browser, you'll see something like this:
<form action="client/insertjob">
There will probably be a whole bunch of other attributes in your form tag - they're not important for this answer.
That action attribute is telling the browser where to go after you click submit. Where is it going? Back to the insertjob method. But it needs to go to your addingjob method - that's where the database update is actually being done. So change the form_open call to:
echo form_open('client/addingjob');
As I see your are using 2 controller functions for posting, page 1 to page 2. You have error on form open you should post your data to addingjob not insertjob.
echo form_open('client/addingjob');
will fix your issue but I highly recommend you to use, one controller for form submit. Below code will send post to same url. And you could add some attributes on it.
<?php
$attributes = array('class' => 'form-horizontal');
echo form_open($this->uri->uri_string(),$attributes); ?>
I have a link in a php while loop
echo "<a href = '#$product_id' onclick = 'pop_up()' id = 'linker'>See more</a>";
The pop up requires the product id to search the database but hash tag is client side. I tried to use javascript window.location.hash but the outcome was not very reliable.
Does anyone know a method preferably server side I could use to retain the active product id while I call the pop up, attain the product id, use it to query the database and output it in the pop up.
I have a session already started and tied to a different condition.
I tried to call the product id directly from the pop up but because of the loop I only get either the first or last in the array.
<?
while ($i < $num) {
$product_id=mysql_result($result,$i,"prod_id");
$title=mysql_result($result,$i,"lTitle");
//main page
echo "<b>" , $title;
echo "<a href = '#$product_id' onclick = 'pop_up()' id = 'linker'>See more</a>";
?>
<!------pop up--------->
<script type="text/javascript">
function pop_up(){
document.getElementById('pop').style.display='block';
}
</script>
<div id="pop">
<p style='color:#6F0A0A; font-size:15px;'><? echo $product_id; ?></p>
</div>
<?
$i++;
}
?>
I'll try answering but to be honest the question is very vague and the code is a bit messy.
First off, you can simply send the product_id as a GET variable to the new popup and read it in PHP. Something like this will work:
echo "<a href = 'http://www.mydomain.com/popup.php?product_id=$product_id' onclick="window.open(this.href, 'popup_win',
'left=100,top=100,width=500,height=500,toolbar=1,resizable=0'); return false;" id = 'linker' >See more</a>";
On your popup.php file (the popup page) you will get the product_id with the PHP $_GET method:
$product_id = $_GET['product_id'];
and then do whatever MySQL query you want, now that you know $product_id.
I hope this helps, if that's not exactly what you meant please add more details so I can revise my answer.
Well, you could first load all this records first and place them into the popup content or, make an ajax request, open the popup, and when the request is done successfully place the values returned into the popup content. Better with JQuery
I'm trying to include an image inside a button using symfony1.4 with this code:
<?php
echo button_to(image_tag('icon.png')."button_name",'url-goes-here');
?>
But the result i get, instead of what i want is a button with "img src=path/to/the/icon.png button_name" as the value of the button. I've google'd it long enought and found nothing, so i'll try asking here.
In other words:
i'd like to find the way to generate html similar to:<button><img src=..>Text</button> but with a symfony url associated in the onclick option
How can i do it to put an image inside a button with symfony? Am i using the helpers wrong?
Thank you for your time!
You are using Symfonys button_to function incorrectly. From the documentation:
string button_to($name, $internal_uri, $options) Creates an
button tag of the given name pointing to a routed URL
As far as I can tell, the button_to function does not allow for image buttons. Instead, you will probably create the button tag yourself and use symfonys routing to output the url.
I finally created my own helper to display this kind of buttons. I know is not very efficient and flexible but works in my case. Here is the code
function image_button_to($img,$name,$uri,$options){
$sfURL = url_for($uri);
$sfIMG = image_tag($img);
if(isset($options['confirm'])){
$confirm_text = $options['confirm'];
$jsFunction = 'if(confirm(\''.$confirm_text.'\')){ return window.location=\''.$sfURL.'\';}else{return false;}';
}else{
$jsFunction = 'window.location="'.$sfURL.'";';
}
$onclick = 'onclick="'.$jsFunction.'"';
if(isset($options['title'])){
$title = 'title=\''.$options['title'].'\' ';
}else{
$title = '';
}
if(isset($options['style'])){
$style = 'style=\''.$options['style'].'\' ';
}else{
$style = '';
}
return '<button type="button" '.$onclick.$title.$style.' >'.$sfIMG." ".$name.'</button>';
}
With this function as helper, in the templates i just have to:
<?php echo image_button_to('image.png',"button_name",'module/actionUri');?>
hope this be useful for someone ;)