Im trying to send 2 arguments to my controller method from my view using GET.
I don't know exactly how to add my 2nd argument to the action URL.
Here I am sending only 1 argument.
action="/MVC/teacher/lookspecificMessage/<?= $details['person_receive_id']; ?>" method="GET">
However, I want to add this <?= $details['related_message']; ?>
I've tried using lookspecificMessage/<?= $details['person_receive_id']; ?>&<?= $details['related_message']; ?> but it dosen't work.
If URL rewriting is used, url should follow declared template.
In your case it can be
action="/MVC/teacher/lookspecificMessage/$person_receive_id/$related_message"
or
action="/MVC/teacher/lookspecificMessage/$person_receive_id/lookrelaredMessage/$related_message"
or any other form.
You should check rewriting rules to find correct template for url.
Using URL rewriting, you can have parameters in the main URL structure like that. Although, typically, GET parameters are passed at the end after a question mark like so:
example.com/path/to/page?param=1
If you want more than one parameter, you use the ampersand to separate them:
example.com/path/to/page?param=1&foo=bar
In your case, it looks like your .htaccess file is doing some URL rewriting or your framework is doing some magic, which is automatically extracting the parameter and setting it in the $_GET array.
And to be clear, here is how you could do yours:
<?php
// Get params
$receiveId = (int) $details['person_receive_id'];
$message = (string) $details['related_message'];
// Build URL
$url = "/MVC/teacher/lookspecificMessage/$receiveId?related_message=$message";
// Encode URL (in case message has weird values)
$url = urlencode($url);
?>
<form method="GET" action="<?php echo($url); ?>">
<!-- REST OF FORM HERE -->
</form>
Related
How can I hide the PHP GET parameters from a URL?
Here is how the URL looks like
../iar7.php?size1=&size=TURF&R3=R3&txtsize=&txttreadd=&small=&large=&smallsw=&largesw=&smallrc=&largerc=&scc=&lcc=&2t1=&2t2o=&2t3o=&2t1=1.36&2t2=1&2t3=5
I want to show only ../iar7.php.
Since you're using a GET, all your payload will be shown as query params. If you would like to hide them perhaps try using a POST instead.
You can read up on some of the differences between the methods here.
If you are using forms, your html form would look like this:
<form method='post' action='/someurl'>
...
As already said before, there are two methods to send data: using GET (which is encoded in the URL), or using POST which means the data is sended as additional payload in the HTTP request. You cannot hide the URL parameters from the GET request method, simply because it is the way GET is supposed to work.
You can do this by specifying this in your <form> tag in the HTML source code:
<form action='the.url.com/path/file.php' method='post'>
<!-- ... -->
</form>
Furthermore I want to add that you have to note that in order to process the data in your PHP file, you will have to call $_POST instead of $_GET.
I'm trying to figure out how to post URL parameters on all href links if a certain URL parameter exists. For example, say I'm on http://myWebSite.com?ID=123, I want all internal or external href links on that page to also carry URL parameter "?ID=123".
Here are some sample href links you might find on my homepage, and with some logic, I want those links to carry the captured URL parameter.
myWebSite.com/about ---> myWebSite.com/about?ID=123
myWebSite.com/contact ---> myWebSite.com/contact?ID=123
twitter.com/cmF ---> twitter.com/cmF?ID=123
if(isset($_GET['ID']))
{
// Post ID URL parameter to all href links
}
You can use the php function output_add_rewrite_var for this purpose.
This function adds another name/value pair to the URL rewrite
mechanism. The name and value will be added to URLs (as GET parameter)
and forms (as hidden input fields) the same way as the session ID when
transparent URL rewriting is enabled with session.use_trans_sid.
Please note that absolute URLs (http://example.com/..) aren't
rewritten.
This can be used like:
<!-- index.php -->
<?PHP
if(isset($_GET['id'])) {
output_add_rewrite_var("id", $_GET['id']);
}
?>
Test url
PHP will rewrite this to the following when calling the script via index.php?id=1234:
Test url
What I have is that when someone clicks a link on my index it calls index.php?action=signup and that will display a signup form.
How do I then cause the signup form to maintain the action=signup portion of the query string so that when the user clicks "Submit" the new query string is index.php?action=signup&email=user#email.com?
What is the best way to set this up? Should I somehow split the actions into GET and the data into POST?
I am not sure if I am describing this correctly but I want to keep all my functions on the index.php so when I integrate .htaccess everything will be like www.site.com/signup, www.site.com/login and so on.
The best way to do this on a form submission is to change the <form> action to the correct URL and the method to GET, like so:
<form action="index.php?action=signup" method="GET">
Then, hidden in your form, you put something like the following:
<input type="hidden" name="action" value="signup" />
Then your URL will contain the correct parameters when it is submitted.
However, if you need to modify this server side, then you can get all of your query parameters via $_SERVER['QUERY_STRING']. Use parse_str to get an array of values, and use http_build_query to recreate the query string:
Example:
$params = array();
parse_str( $_SERVER['QUERY_STRING'], $params);
$params['email'] = 'user#email.com';
echo http_build_query( $params); // Will output a new query string for your URLs
dear all, i'm a noob in codeigniter,
i would like to ask for advise, on how to create a search form that would result to url as:
http://domain/class/index/searchvar1/searcvar1val/searchvar2/searchvar2val
the form would be like
<form action="http://domain/class/index">
<input name="searchvar1" value="searchvar1val">
<input name="searchvar2" value="searchvar2val">
<input type="submit">
</form>
what is the best practice for this?
i've googled around including stackoverflow posts, still can't find any solution.
thanks :)
~thisismyfirstposthere
update ::
i would like to emphasize that my objective is to process search variables from the uri string (above), not from post vars; so i think setting the form search to POST is not an option? :)
update (2)
i don't think this can be done out of the box in codeigniter, i'm thinking of client-processing the form vars/vals into the form action in URI format using jQuery.
will post an update here later when done.
taking a look at the user_guide :
$this->uri->assoc_to_uri()
Takes an associative array as input
and generates a URI string from it.
The array keys will be included in the
string. Example:
$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');
$str = $this->uri->assoc_to_uri($array);
// Produces: product/shoes/size/large/color/red
and i think if your form is this:
<form action="http://domain/class/index/search">
<input name="product" value="shoes">
<input name="size" value="large">
<input name="color" value="red">
<input type="submit">
</form>
then somewhere in the search controller do this $this->uri->assoc_to_uri($data); IMHO will produce product/shoes/size/large/color/red
hey guys, thanks for replying and discussing, I think imma go with flow:
View --> form --> jquery, on form submit: replace action attribute with form params --> controller with uri string --> rest of the process
the jquery part would be something like this
$("#searchFormID").submit(function(){
// $(this).serialize() outputs param1=value1¶m2=value2 string
// var.replace(regex, string) outputs param1/value1/param2/value2 string
// newact would be http://localhost/class/index/param1/value1/param2/value2
var newact = $(this).attr("action") + "/" + $(this).serialize().replace(/&|=/g,"/");
$(this).attr("action", newact); // or $(this).attr("target", newact);
return true;
});
notes:
add an id attribute to the form -> for the form selector in jquery
use post method in the form (this is default in codeigniter) -> so action url will not be populated with the GET string
You may want to enable query strings and use $_GET. IMO, this is a perfect example what the query string is intended for.
Make sure to change your form to <form method="get" action="http://domain/class/index">
You will then have a query string in your URL like ?searchvar1=value1&searchvar2=value2
You can access parts of the string like this: $this->input->get('searchvar1')
With the method you are currently using, if a user searches for any characters that are not allowed in your $config['permitted_uri_chars'], you will have to do a lot of encoding and decoding yourself to make sure the string is safe/readable in your URL. The CI Input class takes care of this for you, so it is my recommendation.
Another disadvantage to URI segment based search params is that there is no proper way to express an array like ?values[]=one&values[]=two&values[]=three. There are hacks to work around this, but they are all exactly that: hacks. you will eventually run into trouble.
Using $_POST is very tedious and forces the use of form submits for paginated results and links, and also ensures that you cannot link to the search results or use the back button properly.
If you must use this method, simply have the form post to your controller, read the input, and then redirect() to the url that you construct from it. littlechad has the right idea for this method in his answer
// This URL:
http://domain/class/index/searchvar1/searcvar1val/searchvar2/searchvar2val
// With this:
$search = $this->uri->uri_to_assoc();
// Produces this:
array(
'searchvar1' => 'searchvarval1',
'searchvar2' => 'searchvarval2',
);
// And you can use it like this:
$arg1 = $search['searchvar1'];
$arg2 = $search['searchvar2'];
To get TO this url, you can do something like this with your form post (in the controller after post):
$var1 = $this->input->post('searchvar1');
$var2 = $this->input->post('searchvar2');
redirect('searchvar1/'.urlencode($var1).'/searchvar2/'.urlencode($var2));
I'm sure you can see how much more work this is than just using the tried-and-true query string...
How do i set the variable that the $_GET function will be able to use, w/o submitting a form with action = GET?
$_GET contains the keys / values that are passed to your script in the URL.
If you have the following URL :
http://www.example.com/test.php?a=10&b=plop
Then $_GET will contain :
array
'a' => string '10' (length=2)
'b' => string 'plop' (length=4)
Of course, as $_GET is not read-only, you could also set some values from your PHP code, if needed :
$_GET['my_value'] = 'test';
But this doesn't seem like good practice, as $_GET is supposed to contain data from the URL requested by the client.
You can create a link , having get variable in href.
<a href="www.site.com/hello?getVar=value" >...</a>
You can use GET variables in the action parameter of your form element. Example:
<form method="post" action="script.php?foo=bar">
<input name="quu" ... />
...
</form>
This will give you foo as a GET variable and quu as a POST variable.
One way to set the $_GET variable is to parse the URL using parse_url()
and then parse the $query string using parse_str(), which sets the variables into the $_GET global.
This approach is useful,
if you want to test GET parameter handling without making actual queries, e.g. for testing the incoming parameters for existence and input filtering and sanitizing of incoming vars.
and when you don't want to construct the array manually each time, but use the normal URL
function setGetRequest($url)
{
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $_GET);
}
$url = 'http://www.example.com/test.php?a=10&b=plop';
setGetRequest($url);
var_dump($_GET);
Result: $_GET contains
array (
'a' => string '10' (length=2)
'b' => string 'plop' (length=4)
)
If you want to fake a $_GET (or a $_POST) when including a file, you can use it like you would use any other var, like that:
$_GET['key'] = 'any get value you want';
include('your_other_file.php');
note: i must add that while this is ok for dev/test/debug, it is considered bad programming and i wouldn't recommend using this method in prod. it would be better to pass the processed values to the function/class instead of having it read the $_GET directly.
The $_GET variable is populated from the parameters set in the URL. From the URL http://example.com/test.php?foo=bar&baz=buzz you can get $_GET['foo'] and $_GET['baz']. So to set these variables, you only have to make a link to that URL.
simply write basic code to set get method value in php
Syntax :- $_GET['< get method variable name>']='';
Ex :- $_GET['send']='invoice';
You could use the following code to redirect your client to a script with the _GET variables attached.
header("Location: examplepage.php?var1=value&var2=value");
die();
This will cause the script to redirect, make sure the die(); is kept in there, or they may not redirect.
For the form, use:
<form name="form1" action="" method="get">
and for getting the value, use the get method as follows:
$value = $_GET['name_to_send_using_get'];
As #Gaurav and #Sander Marechal said, it's possible to add directly at the end of the URL the GET parameters to send to the web page. It works in the majority of cases. But unfortunately, there is an issue with that because an URL cannot accept any character. Some special characters have to be properly encoded to get a valid URL.
Suppose you have an index.php and you want to set a with the value &b= which contains special characters. When I submit the form of the following code, the URL sent is index.php?a=&b= and PHP prints Array ( [a] => [b] => ). So, PHP believes there is two parameters but I only want the parameter a.
You may also notice that the form method is set to POST because if you set it to GET the parameters at the end of the action URL will be ignored. For more details about that see this #Arjan's answer.
<form action="index.php?a=&b=" method="post">
<input type="submit">
</form>
<?php print_r($_GET); ?>
One solution could be to use the PHP function urlencode. But, as you can see in the following code it's not really convenient to use, especially if you have many parameters to encode. When, I submit this form the URL sent is index.php?a=%26b%3D and PHP prints Array ( [a] => &b= ) as expected.
<form action="index.php?a=<?=urlencode("&b=")?>" method="post">
<input type="submit">
</form>
<?php print_r($_GET); ?>
But, instead of urlencode function I recommend using hidden input. Thanks to this tag you can send GET parameters dynamically set by PHP. Besides, these parameters are automatically encoded with the URL standard. And you don't need to use & to separate several parameters: use one input to set one parameter. When, I submit this form the URL sent is index.php?a=%26b%3D and PHP prints Array ( [a] => &b= ) as expected.
<form action="" method="get">
<input type="hidden" name="a" value="<?php echo "&b="; ?>">
<input type="submit">
</form>
<?php print_r($_GET); ?>
I know this is an old thread, but I wanted to post my 2 cents...
Using Javascript you can achieve this without using $_POST, and thus avoid reloading the page..
<script>
function ButtonPressed()
{
window.location='index.php?view=next'; //this will set $_GET['view']='next'
}
</script>
<button type='button' onClick='ButtonPressed()'>Click me!</button>
<?PHP
if(isset($_GET['next']))
{
echo "This will display after pressing the 'Click Me' button!";
}
?>