How to use datepicker in Symfony2 - php

I want to add datepicker in my code. I downloaded bootstrap-datepicke. But how add correct this component? I tried something like this
<form action="{{ path('pattient_test') }}" method="post" >
<input type="text" id="pick" value="02/16/12" data-date-format="mm/dd/yy" class="datepicker" >
<input type="submit" value="PrzeĊ›lij zmienione dane"/>
</form>
And then in controller
var_dump( $this->get('request')->request->get('pick'));
But I still get NULL

Try by adding name attribute to your pick input field,
name="pick"
Also,
var_dump( $this->get('request')->request->all()); Allows you to check all POST parameters, it may help you figure out what's going wrong.

Related

Send form data to the same page not functionning

I'm a newbie in PHP, and I would like to send datas from a form and display it into the same page, here is my code for better understanding:
<form method="post" action="same_page.php">
<input type="text" name="owner" />
<input type="submit" value="Validate" />
</form>
<?php
if(isset($_GET['owner']))
{
echo "data sent !";
}
?>
So normally, after having entered some random text in the form and click "validate", the message "data sent!" Should be displayed on the page. I guess I missed something, but I can't figure out what.
You forgot to add submit name in your form.You are using POST as method so code should be
<form method="post" action="">
<input type="text" name="owner" />
<input type="submit" name="submit_value" value="Validate" />
</form>
<?php
if(isset($_POST['submit_value']))
{
echo '<pre>';
print_r($_POST);
}
?>
Will display your post values
You are using a POST method in your form.
<form method="post" action="same_page.php">
So, change your code to:
if (count($_POST) && isset($_POST['owner']))
Technically, the above code does the following:
First checks if there are content in POST.
Then, it checks if the owner is set.
If both the conditions are satisfied, it displays the message.
You can actually get rid of action="same_page.php" as if you omit it, you will post to the same page.
Note: This is a worst method of programming, which you need to change.
You should Replace $_GET['owner'] with $_POST['owner'] as in your form you have specified method='post'
Replace:
$_GET['owner']
With:
$_POST['owner']
Since you are using the post method in your form, you have to check against the $_POST array in your PHP code.

get custom attribute value in php

I am try to get the value of the input field with a custom attribute I have created using PHP. This is my code:
<form action="uploadform.php" method="post">
<input type="text" name="email" id="email" mynewattribute="myemail">
<input type="submit" name="submit">
</form>
//uploadform.php
<?php
//I know $name = $_POST['email']; will give me the value but I would like to get the value of the input field with "mynewattribute" and not name. Is it possible?
?>
The web browser doesn't know what to do with your custom attribute, so will simply ignore it. The only data sent when you submit the form is the values of "successful" elements. So your custom data will never be sent, and can never be read by the receiving script.
The best place to put such data is into hidden input fields. One possibility is to use names with square brackets in, which PHP automatically converts into arrays. e.g.
<input type="text" name="email[value]">
<input type="hidden" name="email[magic]" value="true">
Populates an array like this:
$_POST['email']['value'] = '';
$_POST['email']['magic'] = 'true';

How can I merge 2 input values from a form before performing a form action?

I am trying to create a form that gets a user directions to a pre-defined location from the location that they entered. I am using Bing Maps 'create a custom url map' to achieve this.
Based on msdn (http://msdn.microsoft.com/en-us/library/dn217138.aspx) I know I need the form to pass a single value such as the following: rtp=adr.'addressfromuser'~adr.'predefined address' . I just don't know how to merge the form values (or insert adr. in front of the user input). I have been unable to find a way to do this so any help is appreciated.
my code for my form is below:
<form action="http://bing.com/maps/default.aspx" method="get" target="_blank">
<label for="rtp">From:</label>
<input class ="input" type="text" name="rtp" value="adr." /> <!--user input-->
<input type="hidden" name="rtp" value="~adr.Mission,TX" />
<!--predetermined destination-->
<input class="btn btn-primary" type="submit" type="submit" value="Get directions" />
</form>
Ding this gets a result that is close to what I want but not quite there (would like to hide the initial "adr." and not generate a "rtp=" before the 2nd rtp being passed. Note: if I comment out the user inputs I successfully get a map with the final destination as point b, but no point a defined
If what I'm trying to do is possible help is appreciated!
You would need to array your ftp input:
EDIT: YOU NEED TO REMOVE adr. in the value="adr." on both rtp inputs!
<form action="http://bing.com/maps/default.aspx" method="get" target="_blank">
<label for="rtp">From:</label>
<input class ="input" type="text" name="rtp[]" value="" /> <!--user input-->
<input type="hidden" name="rtp[]" value="Mission,TX" />
<!--predetermined destination-->
<input class="btn btn-primary" type="submit" value="Get directions" />
</form>
Then, when you process the form, you would implode() that rtp[] array like so:
<?php
/* $_GET['rtp'] will look like this before implosion:
array(
0 => FromCity,UT
1 => ToCity,CA
)
*/
// You need to pre-append the adr. value to both strings by looping array
foreach($_GET['rtp'] as $value) {
$_rtp[] = "adr.'".$value."'";
}
/* Now $_rtp looks like this:
array(
0 => adr.'FromCity,UT'
1 => adr.'ToCity,UT'
)
*/
// Now implode this new array with "~"
// You could assign a new variable to the implode, but if you wanted to,
// you could override the same $_GET['rtp'] variable with it's imploded self.
$_GET['rtp'] = implode("~",$_rtp);
// Now the $_GET['ftp'] = adr.'FromCity,UT'~adr.'ToCity,UT'
?>
use javascript/jquery
$('form').submit(function(){
//do your actions here
$('input[name=rtp]').value()
return false; // prevents submit by default action.
});

Change Response URL without Redirect

Is it possible to change the response URL without performing a redirect?
The slightly longer story...
Due to a limitation with HTML forms the following does not work because the query string from the action URL is not submitted to the server:
<?php // handler.php ?>
...
<form action="handler.php?lost=value" method="get">
<input type="text" name="filter-keyword" value="" />
<input type="submit" name="do-submit" value="Submit" />
</form>
...
So I decided to switch to using the post method and create a separate filter handler script which simply constructs the correct query string for the "handler.php" file.
<?php // handler.php ?>
...
<form action="filter-handler.php" method="post">
<input type="hidden" name="preserve-qs" value="lost=value" />
<input type="text" name="filter-keyword" value="" />
<input type="submit" name="do-submit" value="Submit" />
</form>
...
// filter-handler.php
<?php
$new_url = 'handler.php?filter-keyword=' . $_POST['filter-keyword'];
if (isset($_POST['preserve-qs']) && $_POST['preserve-qs'] != '')
$new_url .= '&' . $_POST['preserve-qs'];
header("Location: $new_url");
?>
Is it possible to achieve this without performing a redirect?
// filter-handler.php
<?php
$qs_args = array(
'filter-keyword' => $_POST['filter-keyword'];
);
if (isset($_POST['preserve-qs']) && $_POST['preserve-qs'] != '')
parse_str($_POST['preserve-qs'], $qs_args);
// Fake query string.
$_GET = $qs_args;
// handler script is blissfully unaware of the above hack.
include('handler.php');
// ???? HOW TO UPDATE QUERY STRING IN BROWSER ADDRESS BAR ????
// Following no good because it redirects...
//header("Location: $new_url");
?>
Yes and no.
No, because to actually really change the URL from the server side you have to make a redirect.
Yes, because there are other solutions to your problem.
Solution no. 1:
Change your code from the first example into:
<?php // handler.php ?>
...
<form action="handler.php" method="get">
<input type="hidden" name="lost" value="value" />
<input type="text" name="filter-keyword" value="" />
<input type="submit" name="do-submit" value="Submit" />
</form>
...
and this should result in proper URL (with lost=value).
Solution no. 2 (ugly one):
Overwrite $_GET array at the beginning of the script to cheat the application into believing the GET parameters were passed.
Solution no. 3 (about changing URL without redirect):
This solution is probably not for you, but it actually imitates changing URL. It is not on the server side, but it actually changes the URL for the user. This is called pushState (demo here) and this is HTML5 / JavaScript feature.
If you can use JavaScript and make AJAX requests, this solution may be perfect for you (you can eg. call whatever URL you like and dynamically pass data you need, even altering the values of the form fields you would submit).

How to display all hidden field values passed from a form all at ONCE in PHP?

Let me explain.
Normally when hidden fields are passed from a form to the page specified in the action of the form, those hidden fields can be accessed on the processing page like so:
The Form:
<form action="process.php" method="POST">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="hidden" name="loginTime" value="1:23PM" />
<input type="hidden" name="userIp" value="173.23.22.5" />
<input type="submit" name="submit" value="Submit" />
</form>
Processing Page (process.php):
<?php
if isset($_POST['submit']) {
echo $_POST['username'];
echo $_POST['loginTime'];
echo $_POST['userIp'];
}
?>
You see how I had to call the two hidden fields by name and individually. Is there any way I can call all hidden fields that are passed to a page from a form all at once despite what the field names of those are or how many there are?
In other words how do I make PHP do this:
// echo the contents of all hidden
fields here (if there were any)
EDIT
Additional info:
The form is designed in such a way (not the one above of course) that field names will be of the following sort:
product_name_1
product_quantity_1
product_price_1
product_name_2
product_quantity_2
product_price_2
and so incremented so on...
Depending on the user action there can be 3 hidden fields or thousands, there are no limits.
Make an array of valid hidden field names, then iterate through $_POST and if the $_POST field name is in the array of valid field names, echo them.
$valid = array('first_name', 'last_name');
foreach ( $_POST as $key => $value ) {
if ( in_array( $key, $valid ) ) {
echo $_POST[$key];
}
}
PHP does not care whether the field was hidden or not, HTTP doesn't tell PHP how it appeared on the website.
The closest thing I would come up with was saving all names of the hidden fields inside an array and echoing them all in a loop.
You can try the following:
<form action="process.php" method="POST">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="hidden" name="group_hidden[loginTime]" value="1:23PM" />
<input type="hidden" name="group_hidden[userIp]" value="173.23.22.5" />
<input type="submit" name="submit" value="Submit" />
</form>
And then print it:
echo htmlspecialchars(print_r($_POST, true));
This might give you a clue about how to solve this.
there's no way to tell the type of the input built-in, so instead you'll need to come up with a way to identify the ones you want yourself. This can be done either by coming up with a special naming scheme, or by storing a list of the names of the hidden fields in another field. I'd recommend the former option, since you don't have the risk of losing data integrity somehow. Look at using array_filter to parse through the array to get the specially-named fields out.
Maybe, assuming that your hidden fields will be in sequence (i.e. 1,2,3 and not 1,2,4) following all of the end users' actions (adding and taking away fields), you could try something along the lines of
$i = 1;
while(isset($_POST["product_name_$i"]))
{
echo $_POST["product_name_$i"];
echo $_POST["product_price_$i"];
$i++;
}
Or something along those lines?

Categories