Hi lets say I'm showing a numeric value in an element (not sure what element to use), what i want to achieve is once the numeric value is clicked (Thinking of onclick="this.form.submit();" or submit button) it will submit different designated value from the numeric value let us say. Apple then my sql query would retrieve apple and use it. NOTE: I have multiple numeric values and multiple designated values for each numeric value as an example it looks like this:
(numeric value) = (designated value)
15123 = apple
24151 = orange
39134 = peach
Here so far is what i have.
<input type='submit' name='searchthem' placeholder='<?php echo $numeric_value; ?>'
value='apple'>
** NOTE i have multiple numeric values with different designated value
And this is the SQL in the same page:
SELECT * from tbl_fruits where fruit_name='".$_POST['searchthem']."' ;
Would appreciate some help and ideas, If there is confusion please comment so i may further clarify.
Use select element and just submit the form so that it can process the values. if you wish to use AJAX, use some javascript and output the result in the browser.
If I understand your problem correctly you should add an array for the definition terms and do it like this:
<input type="hidden" name="searchterm" value="<?php echo $numeric_value; ?>" />
<input type='submit' name='send' value="<?php echo $names[$numeric_value]; ?>" />
Then in PHP switch through the values:
switch($_POST['searchterm']){
case(15123) $term = 'apple';break;
case(24151) $term = 'ornage';break;
case(39134) $term = 'peach';break;
}
This will secure your SQL query, too. [Beware: Never use unfiltered input (i.e. $_POST in your example) from the browser in SQL queries!]
Related
I'm sorry for my English.
I have a dynamic php form connected to mysql DB. In stock array I save the mysql query results. I can access the name of the product[0] and its available quantity [4]. I use the name of the product[0] to set input name and available quantity to fix max parameter value, as it below:
<input type="number" name="<?php echo $stock[$i][0]?>" min="0" max="<?php echo $stock[$i][4]; ?>" value="0" style="width:4em>
I don't kwow how many inputs will be created (and which, because it is dynamic), so I save the name of the input created in another array $_SESSION["INPUTS"] as show:
array_push($_SESSION["inputs"],$stock[$i][0]);
In the next page, I want to $REQUEST the value from the inputs. Now I have the problem, I need to call $REQUEST for the created inputs which names are saved inside $_SESSION["inputs"], I need something as:` $REQUEST["$_SESSION["inputs"]]. I try it different ways without exit.
Any idea? Any alternative?
thanks
I think the best approach is by naming the input name attribute like an array: eg: "name=stock[the product name]". This approach allows you to discard the usage of the $_SESSION["inputs"].
<input type="number" name="stock[<?php echo $stock[$i][0];?>]" min="0" max="<?php echo $stock[$i][4]; ?>" value="0" style="width:4em">
Then to read the values:
$stocks = $_REQUEST['stock'];
foreach($stocks as $productName => $quantity) {
//...
}
This works because the inputs are converted to a php array after being submitted.
You can even get the number of inputs by calling count:
echo count($_REQUEST['stock']);
Please see this link
http://thedesigningworld.com/bea
Here's a Small form contains 8-9 fields + a group of checkboxes
I want to save all details in DB + want to display in a table in proper manner, but it not works properly
Here's the code which i used
for($i=0;$i<count($_POST[wert1]);$i++)
{
if($_POST[wert1][$i]!= "")
{
$check1[] =$_POST['wert1'][$i]; } }
$new1=implode(',', $check1);
$result = "INSERT into table1(check1) values($new1)";
$result = mysqli_query($con, $result);
So i've one doubt that for each checkbox row, should i need to define same array name or different like here i used array name as wert1[] for first row
Checkbox values are not transmitted if the box is not checked.
If you have influence, you could put a hidden input field of the same name before the checkbox and the value "0", like:
<input type="hidden" name="checkbox_name" value="0" />
<input type="checkbox" name="checkbox_name" value="1">Some Text</input>
In you example site, you're using array notation, which is basically a good thing. However, you have not given an index so you might not recognize missing elements.
Well I am Stuck some where here in Array conversion:
My Controller:
$username = $this->input->post('FirstName');
$countryval= $this->input->post('country');
var_dump($countryval);
My View:
<input type="text" name="FirstName" id="Name" >
<?php
foreach ($countryDetails as $row )
{
echo '<input id="country" type="checkbox" name="country[]" class="unique" value="'.$row->country_id.'">'.$row->country_name.'<br/>';
}
?>
<script>
$('input.unique').click(function() {
$('input.unique:checked').not(this).removeAttr('checked');
});
</script>
I am getting the value from checkbox in an array and I need to pass that value as a string further.
I am not able to convert value from array to string i.e if I var_dump($countryval) I get value in array ! Please Help. Thank you
That is because you have used "country[]" as the name in input field
Please try this incase you need a single value to be returned (Edit made here coz I put type="checkbox" instead of "radio".. I have corrected it) Trust radios for single values.
echo '<input id="country" type="radio" name="country" class="unique" value="'.$row->country_id.'">'.$row->country_name.'<br/>';
hope that helps
You need "country[ ]" as name only if it has multiple values. Eg., For a multiple select
<select name="country[]" multiple>
<option></option> <!-- Your options go here-->
</select>
You can use input-checkbox too.. But with your question, I believe you need just a single value to be returned.
Ok.. Now From Your comments I kind of get what you want. Here is my suggestion
1) When you are having multiple checkboxes with the same name, the values are sent as an array.
2) If you would need a single value at a time and definitely need a checkbox, You can make a radio look like a checkbox like this.
3) If you really want to proceed with your javascript allowing only one checkbox at a time, you can do this.
// Load the 'array' helper
$this->load->helper('array');
// Use the 'element' function to return an element from the array
$countryval = element('0', $this->input->post('country')); //should work
Have a try..!!
I'm written a PHP program to display a tab delimited file. The purpose of this is to allow the user to views the rows and select which ones they want by checking the checkbox given in the row for each record. After they hit submit I have a PHP program to display the values, but the problem is only the last row's ID is being passed. However, when the user hits the SUBMIT button I can see all the values for the rows checked:
process_form.php?download=5108&download=5110&download=5114
How should I parse this in process_form.php? I've done a var_dump of $_POST and also
$_REQUEST but it only shows the last value which is 5114. I kind of understand the problem, most of the time in forms programmers only get one value per input field, but what happens when there are many records? It doesn't seem they should all have their own unique 'name'.
<td align=center><input type="checkbox" name="download" value="<?php echo $row['ID']; ?>"></td>
I'm doing something wrong here, but I'm not sure what. Is there a way to pass an array (I'm guessing) of IDs? Or should I be looking at parsing the URL of ?download=5108&download=5110&download=5114 because it has all the values I need there? If so, how do I do that? Thanks!
This is my solution:
<td align=center><input type="checkbox" name="download[]" value="<?php echo $row['ID']; ?>"></td>
Notice that download is now download[], thus creating an array to be passed to the PHP program to process the form.
Then using this demo PHP code I was able to get access to the array:
$my_array = ($_REQUEST["download"]);
print_r($my_array);
echo "<P>";
foreach ($my_array as $value)
{
echo $value . "<BR>";
}
I'd rather use your rowID to identify the name of <input> instead of the value.
<td align=center><input type="checkbox" name="download_<?php echo $row['ID']; ?>" value="1"></td>
Then you can process your request array like:
foreach( $_REQUEST as $key => $value ) {
if( preg_match('/^download_([0-9]+)$/', $key, $reg ) {
$rowId = $reg[1]; // Your row ID
$isChecked = $value; // State of checkbox
}
}
The row ID is parsed from variable name using regexp.
EDIT:
As mentioned in comments, this is not the simplest way to read an array of checkboxes. The simpliest is to name checkboxes download[] and parse this array in PHP then.
However, this is more universal, for example when you need to get array of input texts instead of checkboxes.
This is more of a technique question rather than maybe code. I am having a php form with many fields (items to select). Naturally some of the items might be selected and some not. How do I know which ones are selected when i post the data from page 1 to page 2? I thought of testing each one if empty or not, but there are just too many fields and it doesn't feel at all efficient to use or code.
Thanks,
UPDATE EDIT:
I've tried the following and maybe it will get me somewhere before I carry on testing the repliers solutions...
<html>
<body>
<form name="test" id="name" action="testprocess.php" method="POST">
<input type="text" name="choices[shirt]">
<input type="text" name="choices[pants]">
<input type="text" name="choices[tie]">
<input type="text" name="choices[socks]">
<input type="submit" value="submit data" />
</form>
</body>
</html>
and then second page:
<?php
$names = $_POST['choices'];
echo "Names are: <br>";
print_r($names);
?>
This gives out the following:
Names are: Array ( [shirt] => sdjalskdjlk [pants] => lkjlkjlk [tie]
=> jlk [socks] => lkjlkjl )
Now what I am going to try to do is iterate over the array, and since the values in my case are numbers, I will just check which of the fields are > 0 given the default is 0. I hope this works...if not then I will let you know :)
I think what you're looking for is this:
<form action="submit.php" method="POST">
<input type="checkbox" name="checkboxes[]" value="this" /> This
<input type="checkbox" name="checkboxes[]" value="might" /> might
<input type="checkbox" name="checkboxes[]" value="work" /> work
<input type="submit" />
</form>
And then in submit.php, you simply write:
<?php
foreach($_POST['checkboxes'] as $value) {
echo "{$value} was checked!";
}
?>
The square brackets in the name of the checkbox elements tell PHP to put all elements with this name into the same array, in this case $_POST['checkboxes'], though you could call the checkboxes anything you like, of course.
You should post your code so we would better understand what you want to do.
But from what I understood you are making a form with check boxes. If you want to see if the check boxes are selected, you can go like this:
if(!$_POST['checkbox1'] && !$_POST['checkbox2'] && !$_POST['checkbox3'])
This looks if all the three check boxes are empty.
Just an idea:
Create a hidden input field within your form with no value. Whenever any of the forms fields is filled/selected, you add the name attribute of that field in this hidden field (Field names are saved with a comma separator).
On doing a POST, you can read this variable and only those fields present in this have been selected/filled in the form.
Hope this helps.
Try this.....
<?php
function checkvalue($val) {
if($val != "") return true;
else return false;
}
if(isset($_POST['submit'])) {
$values = array_filter(($_POST), "checkvalue");
$set_values = array_keys($values);
}
?>
In this manner you can get all the values that has been set in an array..
I'm not exactly sure to understand your intention. I assume that you have multiple form fields you'd like to part into different Web pages (e.g. a typical survey form).
If this is the case use sessions to store the different data of your forms until the "final submit button" (e.g. on the last page) has been pressed.
How do I know which ones are selected when i post the data from page 1 to page 2?
is a different question from how to avoid a large POST to PHP.
Assuming this is a table of data...
Just update everything regardless (if you've got the primary / unique keys set correctly)
Use Ajax to update individual rows as they are changed at the front end
Use Javascript to set a flag within each row when the data in that row is modified
Or store a representation of the existing data for each row as a hidden field for the row, on submission e.g.
print "<form....><table>\n";
foreach ($row as $id=>$r) {
print "<tr><td><input type='hidden' name='prev[$id]' value='"
. md5(serialize($r)) . "'>...
}
...at the receiving end...
foreach ($_POST['prev'] as $id=>$prev) {
$sent_back=array( /* the field values in the row */ );
if (md5(serialize($sent_back)) != $prev) {
// data has changed
update_record($id, $sent_back);
}
}