yii2 framework: Controller/Action url & without parameters - php

In my application , I have AdminController with actionupdate, So in YII the path becomes admin/update. Now in order to get certain users info , I use the following path admin/update?id=10 where 10 is the empID.
Is there any way to do the same thing without id part of the path, i.e. I want my path to look like admin/update? instead of (admin/update?id=10). i don't need the user want to see the id values.
Thank you!

You can send data using POST method instead of GET
With the help of javascript
Use an hidden form with post method and a input field. onclick of update button set id to input field and submit form. you will get id in controller's action without showing it in url

use in actionUpdata Yii::$app->user->id;
public function actionUpdate() {
$empID = Yii::$app->user->id;
}

Related

deleting a file from the data base by clint

function appoint_del(sat,sat1)
{
if(confirm("Are You Sure To delete Selected Person Details Completely?"))
{
document.form1.action="student.php?cedit="+sat+"&did="+sat1;//
document.form1.submit();//an alternative to call form
}
}
<?
if($_GET['did']!="")
{
$del=executeupdate("delete from table2 where id=".$_GET[did]);
redirect("student.php?succ=3");
}
?>
To delete the content in data base by clint I have successfully did the job but I am not completelly aware of what is happining by the statement1 document.form1.action="student.php?cedit="+st+"&did="+st1;
and statement2
document.form1.submit()
can any one explane it?
and can sugest any good reference book for clarifing these type of doubts?
document.form1.action="student.php?cedit="+sat+"&did="+sat1;
This line of JavaScript sets a html form's action to the student.php page and appends the two parameters "cedit" and "did" along with their values. I assume you have a form on your page somewhere.
document.form1.submit();
This submits that form. I don't know where to or by what method (POST, GET) because you haven't provided any details on the form. I assume it submits back to the same page because the next line is a handler:
if($_GET['did']!="")
This detects whether the form was submitted by checking if the "did" parameter is present.
$del=executeupdate("delete from table2 where id=".$_GET[did]);
This seems to execute a database query with a massive SQL injection vulnerability. Very dangerous.
redirect("student.php?succ=3");
Redirects back to the same page again, this time passing in a different "succ" parameter which I assume is handled via some other code that you haven't provided.

PHP: How can I retain and use link parameters

I am currently trying to work out a basic referral form. The process is as follows:
The referrer will use a form to send an email to their friends. (Example: http://www.graphicgoldfish.com/refer.html) As you can see, the form requires their Username and their IGN (in-game name). This is very important.
A link is generated using the referrer's information as the parameters. The parameters will be used to reward the referrer once the friend has completed the second form. (Example: http://www.graphicgoldfish.com/referral.html?ref_username=LRRoberts0122&ref_ign=DerpyGoldfish)
When that link is clicked, it opens up a second form where the friend can input their information. My problem is, that when the friend submits the form, the parameters from the original URL do not get passed.
How am I able to keep those parameters after the submission?
My PHP:
<?php
echo $_GET['ref_username'];
echo $_GET['ref_ign'];
if (isset($_GET['ref_username'])) {
$r_username = $_GET['ref_username'];
}
if (isset($_GET['ref_ign'])) {
$r_ign = $_GET['ref_ign'];
}
None of this seems to be doing what I want. My guess is that it doesn't exist, because if I run the PHP and manually add the parameters, it works. I'm just not sure how to go about doing this automagically.
EDIT: The parameters are being passed into an HTML file from the link that was generated. The friend needs to fill out a form. How can I get the values that were passed in the parameters to the HTML file, and concatenate them to the action (where it submits to a PHP file)?
This is not working for me.
<form action="http://www.graphicgoldfish.com/php/referral.php?ref_username=<?php echo $ref_username ?>&ref_ign=<?php echo $ref_ign?>" method="POST">
First off...in your generator.php form. You are using POST not GET
So you should retrive your variables as such.
if (isset($_POST['ref_username'])) {
$r_username = $_POST['ref_username'];
}
if (isset($_POST['ref_ign'])) {
$r_ign = $_POST['ref_ign'];
}
Secondly when you generate that link with the paramters in it. You are passing the parameters to an html file.
http://www.graphicgoldfish.com/referral.html?ref_username=LRRoberts0122&ref_ign=DerpyGoldfish
You need to pass them to a php file for this to work...
http://www.graphicgoldfish.com/referral.php?ref_username=LRRoberts0122&ref_ign=DerpyGoldfish

how to get form method type in Zend framework?

i came across to a kind of dilemma when working on my project in Zend Framework.
When we submit a form, we get the values like this:
$post = $this->_request->getParams();
this will basically capture all the names in a form that is submitted and i can reach the single name value like this:
$key= $post['key'];
And the confusion comes in here when there is a variable value coming from URL, such as:
http://www.mydomain.com/contoller/key/11
so i was to capture the key value from the url i can get it like this again:
$post = $this->_request->getParams();
$key=$post['key'];
My question, how can i differentiate that if this value is coming from URL or from a form?
Or If there is a more secure/reliable way to do this, what would it be?
Thank you
To isolate POST data, simply use
$postData = $this->getRequest()->getPost();
You can also retrieve a single value using
$key = $this->getRequest()->getPost('key');
See http://framework.zend.com/manual/1.12/en/zend.controller.request.html#zend.controller.request.http.dataacess
Although question is about ZF1 but every time I search in google this question comes up for zf2 as well.
So if you wish to get Post types in ZF2 such as GET, POST, DELETE, PUT
You can use following within controller.
$this->request->getMethod(); //return submit type i.e. GET, POST, DELETE, PUT
or
$this->getRequest()->getMethod(); //return submit type i.e. GET, POST, DELETE, PUT
https://zf2.readthedocs.org/en/release-2.2.0/modules/zend.http.request.html
https://zf2.readthedocs.org/en/latest/modules/zend.http.request.html

Get and post in cakephp

In Codeigniter I do this
$p=$this->input->post();
to get all objects posted but I don't know if there is something similar in cakephp to get all posted variables from a form ? I am writing a function to get posted password and save it into database in place of the old password recorded there.
I use native php to get 'posted' variables from a form, (I am not familiar with cakephp form usage) that is why, so instead of using $_POST['sssss'] what should I do now ?
Thank you for any help.
$value = $this->request->data('key');
Please for further reference, read the manual. It's so much easier and better for yourself to figure it out by yourself.
http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-post-data
for the GET method
$this->request->query['category-name'];
and POST method
$this->request->data
http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-querystring-parameters
You can check if posted a form by using
if (!empty($this->data)) {
print_r($this->data);
}
The Post data must be in data to show up in $this->request->data.
Example:
// input field
<input type="text" name="data[foo]" value="bar" />
// in your controller
debug($this->request->data);
To check if posted a form, please use:
if ($this->request->is('post')) {
pr($this->request->data);
}
If you want to get a specific field of the table can move so:
if($this->data["Objetorastreavel"]["id"]){
}
It checks only the ID Objetorestraeval if you want to pick only one field and not post the whole page.
You should able to access form post data with:
For CakePHP 2.x
if ($this->request->is('post')) {
pr($this->request->data);
}
For CakePHP 3.4.x
if ($this->request->is('post')) {
pr($this->request->getData());
}
Documentation for CakePHP 3
You can use following to retrieve post/get data in CakePHP
For post data:
$this->request->data;
For get data:
$this->request->query;

Symfony forms question (restoring selected value of a dynamically populated sfWidgetFormSelect widget)

I am using Symfony 1.3.2 with Propel ORM on Ubuntu 9.10.
I have developed a form that dynamically populates a select widget with cities in a selected country, using AJAX.
Before the data entered on the form is saved, I validate the form. If validation fails, the form is presented back to the user for correction. However, because the country list is dynamically generated, the form that is presented for correction does not have a valid city selected (it is empty, since the country widget has not changed yet).
This is inconvenient for the user, because it means they have to select ANOTHER country (so the change event is fired), and then change back to the original country they selected, then FINALLY select the city which they had last selected.
All of this is forced on the user because another (possibly unrelated) field did not vaildate.
I tried $form->getValue('widget_name'), called immediately after $form->bind(), but it seems (infact, IIRC, if form fails to validate, all the values are reset to null) - so that does not work.
I am currently trying a nasty hack which involves the use of directly accesing the input (i.e. tainted) data via $_POST, and setting them into a flash variable - but I feel its a very nasty hack)
What I'm trying to do is a common use case scenario - is there a better way to do this, than hacking around with $_POST etc?
What I do for this exact issue is that I post the form to the same action that generated it, and in that action, I grab any selected countries/regions/cities as POST variables and pass them back to the template (regardless of validation). In the template, I then use JQuery to set the select values to what they were. When validation passes, they get used. When not, they get passed back to template.
If you can tolerate a little PHP in your JQuery, you could do this in the template:
$(document).ready(function()
{
$("#country-select").val('<?php echo $posted_country; ?>');
});
If you use this approach, don't forget to initialise $this->posted_country in your template the first time around or Jquery will get confused.
I guess you could also use $this->form->setWidget(...)->setDefault(...) or something similar, but I havent found a way around using $_POST as accessing the elements seems to need binding the form otherwise.
UPDATED CODE IN RESPONSE TO COMMENTS BELOW:
if($_POST['profile']['country_id'] != '')
{
$this->posted_country = $_POST['profile']['country_id'];
$q = Doctrine_Query::create()
->select('c.city_id, c.city_name')
->from('City c')
->where('c.country_id = ?', $this->posted_country);
$cities = $q->execute(array(), Doctrine_Core::HYDRATE_NONE);
foreach($cities as $city) $list[$city[0]] = $city[1];
$this->form->setWidget('city_id', new sfWidgetFormChoice(array('choices' => array('' => 'Please select') + $list)));
}
So... I get the country from the post, I query db with that, get cities, and craft cities back into a dropdown. Then in the template, you can set a default selected city with something like $this->posted_city (which would be a POST variable too, if exists).

Categories