PHP - update / refine _GET values - php

Let's say i'm in this page:
search.php?cat=1&powerf=0&powert=0&kilometragef=10&kilometraget=50&yearf=2009&....
and i have all those powerf, kilometraget, etc. input fields and selectboxes in it. How to update selected criterias by clicking a link or a button? I want to return same _GET form but with replaced items (if selected / inputed). Any ideas?
Final variant:
if (isset($_POST['update'])) {
$get = $_GET;
foreach ($_POST as $key => $value) {
if ($key != "update") {
$get[$key] = $value;
}
}
$link = 'search.php?';
$link.= http_build_query($get);
header("Location: http://www.url/$link");
}

You could try something like this:
$get = $_GET;
$get['powerf'] = 'something-else';
$link = 'search.php'
$link.= http_build_query($get);
echo 'Click';

Related

Get the names of $_POST variables by value

If I have several post results that are like this:
$_POST["ResponseA"] = 1, $_POST["ResponseB"] = 1, $_POST["ResponseC"] = 2, $_POST["ResponseD"] = 3, $_POST["ResponseE"] = 1, etc.
How can I perform a loop that gets an array based upon the values? So If I'm checking for a value of 1, I get ResponseA, ResponseB, ResponseE ?
<?php
$results = array_keys($_POST, 1);
var_dump($results);
?>
Use array_flip() like this...
$flipped = array_flip($_POST);
echo $flipped['1']; // ResponseA
You will get issues doing this though as your values are not unique
simple build a loop
<?php
$fields = array('ResponseA','ResponseB','ResponseC','ResponseD','ResponseE')
function searchValue(array $fields, $value) {
$out = array();
foreach($fields as $name) {
if(isset($_POST[$name]) && $_POST[$name] == $value) $out[]=$name;
}
return $out;
}
var_dump(searchValue($fields,1));
You can loop through the $_POST:
foreach ($_POST as $key => $value) {
if ($value == 1) {
// $key will equal the value ResponseA if $_POST["ResponseA"] = 1
}
}

if empty $_POST assign value in foreach loop

Hi want to build a program which creates surveys. I couldn' t figure out how can i assign value for a question which is unanswered. Thank you for your helps.
$dizi = array();
foreach ( $_POST as $key => $value){
if(empty($_POST)){
$_POST="bos";
}
$dizi[$key] = "'".$value."'";
}
Your code doesn't make sense, try this:
$dizi = array();
foreach($_POST as $key => $value) {
if (empty($value)) {
$value = 'your value';
}
$dizi[$key] = $value;
}
$_POST is an associative array
So you can access it with:
$bla = $_POST['bla'];
What you are trying to do is setting the whole array to a string which doesn't work.
You should set the new value when saving it to the $dizi array.
$dizi = array();
foreach($_POST as $key => $value) {
$newValue = $value;
if (empty($value)) {
$newValue = 'bos';
}
$dizi[$key] = $newValue;
unset($newValue);
}
But this only checks if answer string is empty. So this only works if all questions are mandatory.
If I understood you correctly, what you are trying to do is this:
foreach ( $_POST as $key => $value ) {
if(empty($value))
$_POST[$key] = 'This is an unanswered question!';
}
But this cannot work due to the fact that empty values aren't posted from the form.
How do you know that there is 'unanswered' question if it was not posted from the form?
You have to start from the list of the questions (which can not be forged by the user and is defined on the server-side) and check that answer for each of them exists in $_POST. If not - assign whatever you want to the skipped answers.
Try this:
if(isset($_POST) && (!empty($_POST))){
foreach ( $_POST as $key => $value ) {
if(empty($value)){
$_POST="bos";
} else{
//put your code
}
}
}

Looking for a way to store the result of a form field with dynamic name and id in an array after submit

I'm building a form which has the following fields:
$k = 1;
foreach($ui_fmlSect_pg as $val)
{
$url_arr[] = $val;
echo '<tr>';
echo '<td><input size="1" type="text" name="freq_'.$k.'" id="freq_'.$k.'"
value="'.$udf_fmlS.'"></td></tr>';
++$k;
}
I would like to find a way to add the entered values, that is the content of the variable $udf_fmlS or any manual overwriting, from the *freq_$k* -named input fields into an array, but I'm not sure how to proceed with this, especially as the name and id are dynamically generated.
Thanks in advance for your help
You could do something like this:
$freq = array();
foreach ($_POST as $key => $val) {
// check for submitted data starting with freq_
if (substr($key, 0, 5) == 'freq_') {
// use the number after freq_ as the key and the value as the value
$freq[substr($key, 5)] = $val;
}
}
If I understand correctly, you will have variables like:
$_REQUEST['freq_0']
$_REQUEST['freq_1']
etc. You could do something like:
<?php
$freq_val_arr = array();
foreach($_REQUEST as $key => $val){
if (strpos($key, 'freq') === 0){
$freq_val_arr[] = $val;
}
}
Just change 'file_'.$k to 'file[]' or to 'file[' . $k . ']'

Saving post values in a session array

I want to save values send via POST in a session array:
$reply = array('thread_id', 'reply_content');
$_POST['thread_id'] = 2; # test it
$_SESSION['reply'] = array();
foreach ($reply as $key)
{
if (in_array($key, $_POST))
{
$_SESSION['reply'][$key] = $_POST[$key];
}
}
var_dump($_SESSION['reply']);
For example I want to check if the keys 'thread_id' and 'thread_content' are send in post, if they are then I want to save them in a session array called reply, using the same keys.
So for example if 'thread_id' is send via POST:
$_POST['thread_id'] = 'blah';
Then this should get saved in a session called 'reply', using the same key:
$_SESSION['reply']['thread_id'] = 'blah';
How can this be done?
In general, your approach looks valid, but I'm going to guess that you may not be calling session_start(), which is necessary to persist session data.
session_start();
if(!$_SESSION['POST']) $_SESSION['POST'] = array();
foreach ($_POST as $key => $value) {
$_SESSION['POST'][$key] = $value;
}
var_dump($_SESSION['POST']);
in_array($needle, $haystack) checks whether $needle is a value in $haystack and not a key. Use array_key_exists or isset instead:
foreach ($reply as $key)
{
if (array_key_exists($key, $_POST))
{
$_SESSION['reply'][$key] = $_POST[$key];
}
}
Or:
$_SESSION['reply'] = array_merge($_SESSION['reply'], array_intersect_key($_POST, array_flip($reply)));
Use this
$reply = array('thread_id', 'reply_content');
$_POST['thread_id'] = 2; # test it
$_SESSION['reply'] = array();
foreach ($reply as $key)
{
if (isset($_POST[$key]))
{
$_SESSION['reply'][$key] = $_POST[$key];
}
}

$get multiple variables from url

I am making a photography website. In the DB there is an images table, events table and category table which as linked via foreign keys. At the moment i generate events from when a photo was taken from the database and turn them into anchor links.
<?php
while ( $a_row = mysql_fetch_row( $result ) ){
foreach ( $a_row as $field ){
?>
<php? echo $field; ?>
<?php
}
}
?>
When the link is clicked a script gets the variable from get in the url: /images.php?**event**=eventCalledParty
foreach($_GET as $value)
{
$event = $value;
}
$event = mysql_real_escape_string($event);
My question is - If i was to implement categories and have a url that looks like:
/images.php?event=eventCalledParty&category=categoryCalledFashionPhotography
How would i seperate these two variables from the url to use in my queries.
Thank You.
These will automatically show up in these variables...
$_GET['event']
$_GET['category']
PHP does all of this work for you.
$event = mysql_real_escape_string($_GET['event']);
$category = mysql_real_escape_string($_GET['category']);
Each url parameter becomes a separate key in $_GET.
So, for /images.php?event=eventCalledParty&category=categoryCalledFashionPhotography you will get:
$_GET['event'] => 'eventCalledParty'
$_GET['category'] => 'categoryCalledFashionPhotography'
and you can act accordingly:
if ($_GET['category'] == 'categoryCalledFashionPhotography') {
//do stuff here
}
Im a bit rusty on php but I think in your foreach what you want is to get the name of teh field as well as its value.
foreach($_GET as $name->$value)
{
if($name == 'event') {
$event = mysql_real_escape_string($value);
}
else if($name == 'category')
{
$category = mysql_real_escape_string($category);
}
}
foreach($_GET as $key => $value)
{
$event[$key] = mysql_real_escape_string($value);
}

Categories