I know this question was asked before but the answers don't work for me.
My Code:
I have this html select box:
<select name="usergroups[]" id="usergroups" multiple="multiple" style="width:210px;">
<?php
$result = dbselect("SELECT * FROM Groups");
foreach($result as $row){
echo "<option value='".$row['GroupName']."'>".$row['GroupName']."</option>";
}
?>
</select>
The options for the select box come from a database and it worked fine. It shows what should be displayed.
Then i want the selected options to work with them in my php code.
$usGroups = $_POST['usergroups[]'];
$usgr = implode(",",$usGroups);
I convert the array to a string to save the values in my database.
My problem:
It seems that with $_POST I don't get the values and I really don't know why?
Is it because I fill in the options in a dynamic way?
Or is a mistake in the code which I'm not able to see?
It seems so simple but I don't get it.
Thank's for any help! (and sorry for my bad english...)
after submit use:
var_dump($_POST)
die();
or:
var_dump($_REQUEST)
die();
you can as well check like that:
if(isset($_POST['usergroups']) {
//do something
}
to see if the value is in those super globals.
If it's not there then you're not passing it right from the form.
Remember that you need to set method of FORM to POST for this to work ($_REQUEST will have values of either post or get).
Depending on which data you're sending from your form you might need to add enctype="multipart/form-data", i.e.
<form action="/someaction" method="post" enctype="multipart/form-data">
You're using the name of POST variable wrong, you should do it like this:
$usGroups = $_POST['usergroups'];
$usgr = implode(",",$usGroups);
Don't use array brackets to fetch the variable in PHP
Hope this helps!
Related
in codeigniter I have to post an two-dimensional array array[$index][$index] to some variable.
How do I post the value from the array on $val[$index][$index];?
I have to try with some code like this, but didnt work corectly :
$val[$x][$y] = $this->input->post('criteria'[$i][$j]);
Any thoughts on how I should try to get this working?
Thanks!
In an html Form you can add a field with name having double square brackets. Like:
<input name="myVal[][]" />
Then post this into a form to php.
In you php file write:
echo '<pre>';print_r($_POST);'</pre>';
You will see posted fields there. You can get this posted data in codeigniter using:
$this->input->post('myVal');
Thanks in advance for any suggestions on the following:
I've created a php-page to add works from composers to CDs in a database. It's a combination of two forms and looks like this:
1st form:
Composer : [drop down list] : Select
Some blank space
2nd form:
Title : [drop down list]
Track number : [empty varchar field]
Work : [drop down list]
some other fields
Process button
After selecting a name in the first block (posting to $_SERVER["PHP_SELF"]) I stay on the same page, that name is shown in the blank space in between and the drop down lists are populated with only CD titles and works of the selected composer.
I can then select from the lists and enter other data in the other fields. Hitting the process button posts the data from the second block to another page which will eventually send everything to a table in a MySQL database.
After that I send myself back to the first page with header("Location: the_first_page.php")
So far so good, but upon returning I would like the composer, title and work to be preselected. Now I'm sent to a blank page and have to start from scratch. I think I've seen some solution involving testing $_POST['something'] against <option value> in a drop down list but I can't seem to make that work.
My question is: Is there a way to send $_POST['Title'] and $_POST['Work'] back to the first page somehow? Or is it better to split the two forms over seperate pages?
All help is welcome.
You could use sessions or the post data itself. For using the post data itself, the page where you send the request should be the same and include the script that will process it if there's some data like this:
if (!empty($_POST)) {
include "save.php";
}
// More code...
?>
<select name = "a">
<option <?php if ($_POST['a'] == "test") echo "selected"; ?> value = "test">
<option <?php if ($_POST['a'] == "testb") echo "selected"; ?> value = "testb">
</select>
Of course there are many more ways, but this is just a simple one to get you started. Things to know: you might want to change the variable $_POST and clean it up before using it. In this case it should be fine, but in <input type = "text" value = "<?= $_POST['b']; ?> name = "b">` you have a serious security issue.
For sanitizing the input, you want to sanitize it respect to what you expect. But also, as an EXTRA meassure, you normally want to strip everything that looks like a <script>, onclick ="", ' DROP TABLE users and similar. It's not an easy subject, so I recommend you reading on it, mainly on the XSS attacks which is relevant to showing the text back to the user. While it might seem too much work for this "simple case", it is useful in many more situations.
Use session variables and put conditions for them ... see [ $_SESSION ].
I'm working on a category section for stories submitted to my website. I have a html select going on in my html and I have that submitting to the database using codeigniter functions. It is submitting to the database, but there seems to be a small problem...the select is only grabbing the last value that is selected. I need to have all the values that are selected entered into the database so I can pull them out at a later date.
This is the html I am using.
<select multiple class="multi" name="wGenre">
<option value="Action/Adventure">Action/Adventure</option> <option value="Angst">Angst</option>
<option value="Crime">Crime</option>
</select>
and then this is what I have in my model.
$this->title = $this->input->post('wTitle');
$this->genre = $this->input->post('wGenre');
$this->db->insert('story_tbl', $this);
Now, I believe the problem is with my database. I originally set up the field as an enum, but that won't work because it's either one or the other selection and I need to have multiples. So then I tried it as a text field, and it's just not grabbing everything. To be honest I'm at a loss and in need of some help. :)
Firstly you need to make sure the select tag is named the array manner:
<select multiple="multiple" class="multi" name="wGenre[]">
Then To get all values of the multiple select you could do the following to save the values in an array!
$this->title = $this->input->post('wTitle');
$select_vals = array();
foreach($this->input->post('wGenre') as $val){
$val.' - '; // Used as a delimiter for later use
array_push($select_vals, $val);
}
$this->genre = $select_vals;
$this->db->insert('story_tbl', $this);
I ended up solving this without a foreach loop.
First of all, I was writting my html select tag wrong. For it to select multiple values you have to add both [] at the end of the class and a muti tag...like this.
<select multiple="multiple" class="multi" name="wGenre[]">
and then in the php it has to be json encoded to be placed in the database correctly...like this...
$this->genre = json_encode($this->input->post('wGenre'));
Thank you for all your help! I hope this helps someone else in the future! :)
I hope I'm not posting a duplicate question but I've looked around (and googled as well!) and nothing has given me the answer I'm looking for.
I have a form in HTML. When the user submits the form the values get stored with mysql under their user account for the site.
The issue is, I'd like the user to be able to go back and edit the form any time they like.
I could certainly just populate the form with values from php when the users review the form, but it gets tricky when I try to populate a file input field (and the file has been saved in mysql using the blob type). Not to mention that I'd like to do this as cleanly as possible.
Ideally it would be nice if there was a convenient module for reviewing forms that have already been submitted in JQuery per se.
Can anyone offer any advice? Thanks in advance!
Edit:
Here's a good example of what I mean - in chrome if I fill out a form and redirect to the next page after hitting submit, if I hit back I come back to the form and it's still filled out with the information I entered previously! Could I invoke this behaviour whenever I want to, as opposed to only when the user hits back?
You can't pre-fil an <input type="file" . . but surely when they come back to the form, they want to see the file they've uploaded .. this is what you mean right ..
So if its a picture, you could just do: <img src="loadpic.php?id=$var" />
If it's files they've uploaded, just list the file name / date and other data.. etc in some sort of list.
Then you could still show the <input type="file"> .. but with the label, 'add more pictures' or 'add another file'. .etc
Unless someone has a better way, at the moment I'm using a combination of 2 things:
1) Utilizing the $_SESSION variable
2) Setting the "name" attribute of every input in the form to the name of the field it corresponds to in the database.
This way I can loop through all the values dynamically instead of hardcoding them all in. Some input types (like file) are exceptional and will be handled on their own. Other that I can do something like this:
To insert into mysql:
$fields = array();
$values = array();
foreach ($_POST as $field => $value) {
$fields[] = $field;
$values[] = addslashes($value);
}
$fieldString = 'Table_Name('.implode(', ', $aFields).')';
$valueString = "VALUES('".implode("', '", $aValues)."')";
mysql_query("INSERT INTO $fieldString $valueString");
Reviewing the form is somewhat similar. I am using javascript to hook into document.onload. I need to pass javascript the records from mysql so that it may populate the form. Then it's a simple matter of getting elements by their name and assigning them their values that were passed from php.
The easiest way to do it and not have to go back to the database would be to store the values in a session.
<?php $_SESSION['myvalue'] = $inputvalue; ?>
On the html form use:
<input type="text" name="myName" value="<?php echo $_SESSION['inputvalue']; ?>" />
When completed don't forget to unset the session variable:
<?php session_start(); unset($_SESSION['myvalue']); ?>
I swear i couldn't find a simple working solution for this.
On a form i have inputs that have names containing "[]" and i cant change the names of the inputs because they are part of a script.
I want to php POST the values of those inputs at the next page, after the form submit.
Example of input
<input type="text" name="CustomFields[13]" id="CustomFields_13_1" value="">
Anyone knows how to accomplish it?
I want to do it using PHP only
If the name is CustomFields[13], then you can access it at $_POST['CustomFields']['13'].
You "cannot" POST something with PHP. It's always the client that POSTs to the server. PHP is running on server side.
I recommend that you use sessions and save there the values that you need to have available in next pages.
This is how you set a session:
session_start();
$_SESSION['CustomField'] = "test";
And this is how you get it:
session_start();
echo $_SESSION['CustomField']; //Should display "test"