Array to string convertion Error Cakephp - php

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'));
?>

Related

Appending GET parameters on HREF tag with PHP

I'm working on a code like this:
<?php
$id=$_POST['id'];
$url_tag = $_POST['url_tag'];
$url_back = 'https://www.page.example.com/page.php?';
$query='id='.$id.'&url_tag='.$url_tag;
$url = $url_back.$query;
echo 'Look how this url shows up: '.$url;
echo '<a href='.$url.'>Click here</a>';
?>
This is, the page receives two POST parameters. Then prepare a link to https://www.page.example.com/page.php? and I append those two parameters as GET parameters with the ids id and url_tag respectively.
Then I display how the whole link looks like. It shows up correctly, in this case https://www.page.example.com/page.php?id=ID&url_tag=URL_TAG, where ID and URL_TAG are the actual values received as POST parameters.
However, when I click on the 'Click here' link, it redirects me to https://www.page.example.com/page.php?, which is the url without any GET parameter.
Why is that happening and how would I solve it? I've tried to feed HREF with urlencode($url) instead, but it redirects me to an address flooded with undesired characters...
Any idea? Thank you!
Try to replace the last line of your code by this:
echo 'Click here';
It should work.
Try using http_build_query(), it takes care of any URL character compatibility issues for you...
// assuming you've already checked and validated your $_POST parameters
$query = http_build_query(array(
'id' => $_POST['id'],
'url_tag' => $_POST['url_tag']
));
$url = 'https://www.page.example.com/page.php?' . $query;
?>
Click here

Unable to insert data with CodeIgniter

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); ?>

cant get values from form

In consequence from yesterdays post as i was trying to transfer some variables between the controller and the view today I am trying to get data from a form and update the db but am having trouble getting those values.
this is function from the controller which is called from the form of the view
function updateRecords(){
$data2=array('name'=>$this->input->post('first_name'),
'surname'=>$this->input->post('last_name'),
'contact'=>$this->input->post('contact'),
'email'=>$this->input->post('email_address'));
print_r($data2);
}
when I try to print the data2 array I get: Array ( [name] => [surname] => [contact] => [email] => )
this is the code from the view:
<fieldset style="text-align:left">
<legend><h2>Edit Clients Details</h2></legend>
<?php
$this->load->helper('form');
echo form_open('site/updateRecords');
echo form_input('first_name', $records['0']->name);
echo form_input('last_name', $records['0']->surname);
echo form_input('contact', $records['0']->contact);
echo form_input('email_address', $records['0']->email);
echo validation_errors('<p class="error">');
echo anchor('site/updateRecords','Save');
echo form_close();
?>
</fieldset>
<p>
<?php echo anchor('site/add','Add clients');?>
<?php echo anchor('site/members_area','Go Home')?>
<?php echo anchor('login/logout', 'Logout'); ?>
instead of
echo anchor('site/updateRecords','Save');
try use
echo form_submit('mysubmitname', 'Save!');
anchor can't submit form data by default. If you use ajax, create javascript function which serialize form data and post to the server.
i hope this help
There are a couple of ways to have the id be accessible. One is to structure the edit page call so that it has the id in it. Normally I do this by setting up my urls to be something like: http://site.com/client/client_id/edit for the edit page. Alternately, you can stash the record's id in a hidden form field when you call the form up and then pass it back as part of the post.

Adding JS To Codeigniter Links - Simple OnClick

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.

URL as a Variable in PHP

I have a cool project where I need to upload an image via php/my_sql. That I can handle, but the images need to be linking to a certain url out of 100. In php can I save a url as a variable, then allow a drop-down menu of the 100 choices which point to a variable with a url?
The best choice would be to use an array:
$urls = array("url","url2","url3");
After you add all the 100 URLs in there, you can recurse through the array and output options into the tag.
<?php
echo "<select>";
foreach($urls as $current_url){
echo "<option>" . $current_url . "</option>";
}
echo "</select>";
?>
That would go through the array, echoing all the URLs into the tag.
If you don't want to set the text in the dropdown to the actual URL, you could set the array using keys array("This URL" => "url") etc. and put the URL value into the "value" property of the tag, and using the key name as the value between the opening and closing tags of the list.
If you need an explanation of that as well, I can provide one.
I'm still not sure what you mean, but if you want to know how to store a url in a variable that is usually done in a string like this:
$url = "http://www.mysite.com/the/beautiful/image.gif";
You can also redirect to that url like this:
header('Location: '.$url);
die();
If you want the user to decide to which site to go, do it similar to what BraedenP posted:
<select id="urls" onchange="document.location.href=document.getElementById('urls').options[document.getElementById('urls').selectedIndex].value;">
<?php
$urls = array(
'Image One' => 'http://www.mysite.com/one.gif',
'Image Two' => 'http://www.mysite.com/two.gif',
'Image Thee' => 'http://www.mysite.com/three.gif'
);
foreach($urls as $name=>$url){
echo "<option value=\"{$url}\">{$name}</option>";
}
?>
</select>

Categories