I need to insert pairs of input values from a form into mysql database. For example here are my input boxes:
<input type="text" name="roomType1" size="30" />
<input type="text" name="roomRate1" size="30" />
<input type="text" name="roomType2" size="30" />
<input type="text" name="roomRate2" size="30" />
<input type="text" name="roomType3" size="30" />
<input type="text" name="roomRate3" size="30" />
etc..
And my sql database is set up as follows:
RoomType
RoomRate
HID
So basically I need to figure out how to pass the two input fields into each field together in the same row. I am not sure if I should do a for loop or how I can get each two and insert it with the same ID. I hope this makes sense. and any help would be GREATLY appreciated!
Those inputs must be part of a form with method="post". Then, on the php side use this:
<?php
$i = 1;
while(isset($_POST['roomType'.$i]))
{
$roomType = $_POST['roomType'$i];
$roomRate = $_POST['roomRate'$i];
// perform your sql statements here
$i++;
}
?>
Since your inputs are paired, it's enough to check if only one of them exists;
Related
This is the output of my code
Every item has its own item number. I will be entering randomly a value for 3 items, after doing so I will click on the add button and will be redirected to a confirmation page. How will I get the values with its corresponding item?
Thanks in advance for the answers :)
Use input name as array.
<input type="text" name="price[]" />
<input type="text" name="price[]" />
<input type="text" name="price[]" />
<input type="text" name="price[]" />
<input type="text" name="price[]" />
Get values like this
$price = $_POST['price'];
foreach($price as $key => $val ) {
echo $val;
echo "<br>";
}
You can bind or keep the text box name same as your database row ID so when you submit and in the target page you can redo the select and update the field in the database like
<input type="text" name="$id" />
in the target page use Select Query looping and give
Update table_name set field_name=$_REQUEST[$id] where id=$id
I have a form and there is a button to append another set of input boxes if you wish to add more information. Everytime it adds a new set of boxes all the input boxes get a unqiue number added on for that set of input boxes.
Example:
If you have three sets of input boxes it would look like this:
name, age, gender, dob
name1, age1, gender1, dob1
name2, age2, gender2, dob2
However, when I send this information over to my php file I extract the information from the array so each one is a variable. So, name would be $name and name1 would be $name1 and so on. But my question is how can I sanitize and validate all the names at once and all the ages at once etc..
The reason I am asking is because I have googled this alot and I can't find an answer on how to do this.
Try to create sets as given in sample below:
For first set:
<input type="text" name="name[]" id="name1" />
<input type="text" name="gender[]" id="gender1" />
<input type="text" name="age[]" id="age1" />
<input type="text" name="dob[]" id="dob1" />
For second set:
<input type="text" name="name[]" id="name2" />
<input type="text" name="gender[]" id="gender2" />
<input type="text" name="age[]" id="age2" />
<input type="text" name="dob[]" id="dob2" />
and set all the further sets accordingly.
Now, to get posted data you can use
<?php
echo "<pre>".print_r($_POST, true)."</pre>";
?>
You may use something like this for each entity:
<input type="text" name="age[]" id="age1" />
Here, id should be in incremental order with JavaScript or jQuery and name should be same which will give you an array for all the attributes in $_POST or $_REQUEST
Print $_REQUEST and you will come to know how exactly you can get all the data.
You are already getting the array for the name, age, etc.
To sanitize use array_map() function in php. It will sanitize the array.
EXAMPLE
$result = array_map("stripslashes", $result);
Here $result is an array
How can i add multiple text fields in one array?
My code
HTML
<form method="post">
<input type="text" class="span6" name="hashtags" />
<input type="text" class="span6" name="hashtags" />
<input type="text" class="span6" name="hashtags" />
<input type="text" class="span6" name="hashtags" />
</form>
PHP
$arr = array($_POST['hashtags']);
$arraz = serialize($arr);
$a = addslashes($arraz);
SQL
INSERT INTO table(hashtags) VALUES ($a)
And it inserts value only from last text field, how can i insert all text field values?
If you're going to have the inputs share a similar name and you want to retrieve them as an array, you have to have the following construction. Notice the [] at the end of the name.
<input type="text" class="span6" name="hashtags[]" />
Use [] to make hashtags an array:
<input type="text" class="span6" name="hashtags[]" />
...
...
...
<input type="text" class="span6" name="hashtags[]" />
Now $hashtags is an array in the $_POST array:
$arraz = serialize($_POST['hashtags']);
$a = addslashes($arraz); //need to use mysqli_real_escape_string() or something
Also, that's not a good idea to store them all in one column either serialized or delimited. Store them individually.
I have 3 text fields and I want to pass the values after combining them using a hyphen.
<input type="text" name="val[]" />
<input type="text" name="val[]" />
<input type="text" name="val[]" />
Preferably help me with php implode option.
How do i retrieve it after submit ?
Thanks.
After sending the form, your values will be in $_POST['val'] or $_GET['val'] as an array, depending on the method of your form.
You can combine them simply by:
$hyphenated = implode("-", $_POST['val']); // or $_GET['val']
thanks. how do i change focus to next field once a field has max values:
See if this works:
<input type="text" name="val[]" onkeyup='checkVals("field1", "field2");' id='field1'>
<input type="text" name="val[]" onkeyup='checkVals("field2", "field3");' id='field2'>
<input type="text" name="val[]" id='field3'>
<script>
function checkVals(this_field, next_field){
var fieldval = document.getElementById(this_field).value;
var fieldlen = fieldval.length;
if(fieldlen > 10){ // you can change 10 to something else
document.getElementById(next_field).focus();
document.getElementById(next_field).select();
}
}
</script>
<form action="test.php" method="post">
Name: <input type="text" name="fname" />
<input type="hidden" name="fname" value="test" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
How can I read the values of both the fields named fname?
On my action file(test.php) under $_POST, I am getting only hidden field value.
Is there any PHP setting through which I can read both values?
I believe you want to name the fields as:
Name: <input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
to make PHP understand them as an Array.
In case someone wants to do this and doesn't want to change the name of the form elements, or can't, there is still one way it can be done - you can parse the $_SERVER['QUERY_STRING'] or http_get_request_body() value directly.
It would be something like
$vals=explode('&',http_get_request_body());
$mypost=Array();
foreach ($vals as $val) {
list($key,$v)=explode('=',$val,2);
$v=urldecode($v);
$key=urldecode($key);
if ($mypost[$key]) $mypost[$key][]=$v;
else $mypost[$key]=Array($v);
}
This way $mypost ends up containing everything posted as an array of things that had that name (if there was just one thing with a given name, it will be an array with only one element, accessed with $mypost['element_name'][0]).
For doing the same with query strings, replace http_get_request_body() with $_SERVER['QUERY_STRING']
If you want to pass two form inputs with the same name, you need to make them an array. For example:
<input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
You can then access it using the following:
$_POST['fname'][0]
$_POST['fname'][1]
You might want to rethink whether you really need to use the same name though.
Solutions are
1) Try using different name for textbox and hidden value
2) Use an array as mentioned above for field name
3) Its not possible as the values will be overwritten if the names are same