In on of my views i have the following form:
<table align="center" border="1">
<tr>
<td>Identificação</td>
<td>Conc, %</td>
<td>Classificação 67/548/CEE</td>
<td>Classificação 1272/2008 (CLP)</td>
</tr>
<tr>
<td>
<textarea rows="4" cols="30" name="componentes[0][identificacao]"></textarea>
</td>
<td>
<textarea rows="4" cols="30" name="componentes[0][conc]"></textarea>
</td>
<td>
<textarea rows="4" cols="30" name="componentes[0][classificacao_cee]"></textarea>
</td>
<td>
<textarea rows="4" cols="30" name="componentes[0][classificacao_clp]"></textarea>
</td>
</tr>
</table>
<div id="outro_curso"></div>
<p class="submit">
<button id="novo_curso">Add Curso</button>
</p>
As you can see, I'm using multidimensional arrays. I'm doing that, because when I click the "Add Curso" button I call a jquery function that generates another table like the previous one. The names of the text areas will be componentes[1][identificacao], componentes[1][conc], etc...
NOTE: I can use the button to generate tables the number of times I want.
Now, my problem is how to get this data treated in my CodeIgniter model. I tried saving the data to the $componentes array (to later insert into the database) But I guess there is something wrong with my code:
$componentes;
foreach ($this->input->post('componentes') as $key => $value){
$componentes[$key]['identificacao']=$value[$key]['identificacao'];
$componentes[$key]['conc']=$value[$key]['conc'];
$componentes[$key]['classificacao_cee']=$value[$key]['classificacao_cee'];
$componentes[$key]['classificacao_clp']=$value[$key]['classificacao_clp'];
}
Can someone give me a little help please?
EDIT:
I forgot to mention, I'm getting the error:
Invalid argument supplied for foreach().
So I don't know if my foreach ($this->input->post('componentes') as $key => $value){ is correct or if is some problem in the lines inside.
$componentes_post = $this->input->post('componentes');
if(is_array($componentes_post)&&!empty($componentes_post))
{
foreach ($componentes_post as $key => $value)
{
$temp_componentes['identificacao']=$value['identificacao'];
$temp_componentes['conc']=$value['conc'];
$temp_componentes['classificacao_cee']=$value['classificacao_cee'];
$temp_componentes['classificacao_clp']=$value['classificacao_clp'];
$componentes[] = $temp_componentes;
}
}
Now see,
print_r($componentes);
You should get what you need.
Next, you are getting error Invalid argument supplied for foreach() since the array you have iterated on foreach is empty, make sure value is coming on that element by using print_r($this->input->post('componentes'))
Related
I'm trying to loop through an array inside of an array, so that I can display either phone numbers or extension number. For example: If a user has both (phone number and extension number) then I should ONLY display phone number, but sometimes a user has only a extension number then I should display the extension number.
And here's my code:
<table style="padding: 40px;margin-left: -10px;margin-top:-38px;display: inline-block;">
<div style="margin-top:16px;margin-left:10px;">
<input type="checkbox" id="checkAll"/>
</div>
<div style="padding:20px;">
#foreach($resultArray as $key => $value)
#foreach($value as $key2 => $value2)
#if(is_array($value2))
#foreach($value2 as $key3 => $value3)
<?php
// echo var_dump($value3);
if (in_array($value3['phoneNumber'], $value3)) {
if (strlen($value3['phoneNumber']) === 11) {
$value3['phoneNumber'] = ltrim($value3['phoneNumber'], 1);
}
}
else{
$value3['phoneNumber'] = $value3['extension'];
}
?>
<tr>
<td>
<input class="input_checkbox" type="checkbox"
id="{{$key3}}customer-name-checkbox" name="{{$key3}} "
value="yes"><span style="padding-left:40px;"></span>
</td>
<td>{{$value3['firstName']}} {{$value3['lastName']}}</td>
<td>{{$value3['phoneNumber']}}}</td>
<td><input style="margin-left:60px;float: right;" type="email" class="styled-text rounded" name="{{$key3}}" id="{{$key3}}customer-name-inputField" placeholder="" value=""/><br/><br/>
</td>
</tr>
#endforeach
#endif
#endforeach
#endforeach
</div>
</table>
Can someone tell me what I'm doing wrong please? Thank you so much in advance!!
From your requirements, I think you need something like this:
<?php
foreach ($resultArray['searchUserResults']['searchUserResult'] as $key => $data)
{
if (isset($data['phoneNumber']))
echo $data['phoneNumber'];
else if (isset($data['extension']))
echo $data['extension'];
}
?>
This will output either of the two, but only phoneNumber if both are present.
You do not need all the nested foreach loops to accomplish this. Instead you iterate over the sub-array only.
Sorry I'm a bit of a noob when it comes to PHP but I just wondered if someone had an idea on how I could solve this PHP/SQL problem.
I have a PDO statement that gets all users from a database.
With the array of users from the database I create a foreach loop to display all of the users in a table which I want to use to select a specific user, enter a number in the row of the user I select, then click submit and store the users name and also the number. I will use this information to populate another database later.
My question is, I cant seem to reference the user or the number in the table to extract the user and number I enter. When I try and request the numbered entered in the index.php, it will only ever display a number if I enter a number for a the final user in the table. When I try and view the FullName it never works and I get 'Undefined index: FullName' error.
I also specified to 'POST in the form but it doesnt seem to be doing that.
Does anyone have any ideas?
Thanks
//function.php
function getName($tableName, $conn)
{
try {
$result = $conn->query("SELECT * FROM $tableName");
return ( $result->rowCount() > 0)
? $result
: false;
} catch(Exception $e) {
return false;
}
}
//form.php
<form action "index.php" method "POST" name='form1'>
<table border="1" style="width:600px">
<tr>
<th>Name</th>
<th>Number Entered</th>
<tr/>
<tr>
<?php foreach($users as $user) : ?>
<td width="30%" name="FullName">
<?php echo $user['FullName']; ?>
</td>
<td width="30%">
<input type="int" name="NumberedEntered">
</td>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" value="submit"></td>
</form>
//index.php
$users = getName('users', $conn);
if ( $_REQUEST['NumberedEntered']) {
echo $_REQUEST['NumberedEntered'];
echo $_REQUEST['FullName'];
}
The variable FullName isn't transmitted by your form to index.php. Only values of form elemnts are sent. You can add a hidden form field, that contains FullName like this:
<input type="hidden" name="FullName" value="<?php echo $user['FullName']">
But your second problem is, that your foreach loop will create several input fields with the exact same name. You won't be able to recieve any of the entered numbers, except the last one. have a look at this question for possible solutions.
Update
Putting each row in individual form tags should solve your problem:
<?php foreach($users as $user) : ?>
<form action="index.php" method="POST">
<tr>
<td align="center" width="40%" >
<?php echo $user['FullName']; ?>
<input type="hidden" name="FullName" value="<?php echo $user['FullName']; ?>" />
</td>
<td width="30%">
<input name="NumberedEntered"/>
</td>
<td>
<input type="submit" value="submit"/>
</td>
</tr>
</form>
<?php endforeach; ?>
Dunno if the title makes sense, but I have a variable which would to put it in basic terms would be called like this:
$_POST['something'+$variable2]
I have a form which is for editing selected records, this form contains entries for all previously selected records:
<form name="input" action="editcar.php" method="POST">
<input type="submit" value="Yes">
while($row = mysqli_fetch_assoc($result))
{
echo'
</div>
<table style="color:white">
<tr>
<td style="text-align:right">Manufacture:</td><td><input type="text" name="manufacture'.$row['carIndex'].'" value="'.$row['make'].'"></td>
<td style="text-align:right">Model: </td><td><input type="text" name="model'.$row['carIndex'].'" value="'.$row['model'].'"></td>
</tr>
<tr>
<td style="text-align:right">Colour: </td><td><input type="text" name="colour'.$row['carIndex'].'" value="'.$row['colour'].'"></td>
<td style="text-align:right">Reg: </td><td><input type="text" name="reg'.$row['carIndex'].'" value="'.$row['Reg'].'"></td>
</tr>
<tr>
<td style="text-align:right">Price: </td><td><input type="text" name="price'.$row['carIndex'].'" value="'.$row['price'].'"></td>
<td style="text-align:right">Mileage: </td><td><input type="text" name="mileage'.$row['carIndex'].'" value="'.$row['miles'].'"></td>
</tr>
<tr>
<td style="text-align:right">Max MPH: </td><td><input type="text" name="mph'.$row['carIndex'].'" value="'.$row['mph'].'"></td>
<td style="text-align:right">MPG: </td><td><input type="text" name="mpg'.$row['carIndex'].'" value="'.$row['mpg'].'"></td>
</tr>
</table>
</form>
</div> ';
}
?>
</form>
The form is looped for each record previously chosen, to enable mass editing. The isue arouses when I realised I'd have multiple inputs with the same name, so I did:
<input type="text" name="model'.$row['carIndex'].'" value="'.$row['model'].'">
Placing the primary key of the record it was currently tired to on the end of it's name. Which seemed like a logical way to go about things.
However now I need to call these variables to place in the mysql query and I dunno how to do that, or even if I can.
I have the selected records saved in an array so I have:
foreach ($postid as $carID)
{
$query = "stuff";
mysqli_query($db, $query);
}
Each loop has $carID containing the variables that was put on the end of the form input names.
So something like:
$_POST['something'+$variable2]
is all I can think of but doesn't work.
Any method that works for my overall code is welcome not just a solution to the issue I've made.
Actually your way should work. Just replace the + with . in $_POST['something'+$variable2].
My tip is: use an array as name in your html instead:
<input type="text" name="model[]" value="'.$row['model'].'">
On php-Side you can loop through all $_POST['model'] since its an array now.
You can add the index for every entry in your html, too:
<input type="text" name="model['.$row['carIndex'].']" value="'.$row['model'].'">
PHP uses a dot for concatenation, not + like Java and Javascript:
$_POST['something' . $variable2]
Try something like this:
<form ...>
<?php
while($row = mysqli_fetch_assoc(...):
$index = $row['carIndex'];
?>
<input type="text" name="carmodel[<?php echo $index?>][model]" value="<?php echo $row['model'] ?>">
<?php endforeach; ?>
</form>
This way you will have the data stored in $_POST['carmodel'] as an array indexed by carIndex value as the structure of data in $_POST is defined by names of inputs, here you will have names likee carmodel[1][model] for example so then in post it will be in $_POST['carmodel'][1][model]
you can read here as well
How would I create this array structure in an HTML form?
I think that there is either an error in my code, or my PHP or Apache is set up incorrectly.
When I submit a form with a hidden field in it, I do not get any data in my $_POST array...
When I comment out the hidden field in my code, the POST data is returned correctly...
HTML FORM
<form action='/utils/login.php ' method='POST'>
<table>
<tr>
<td colspan='2'>
Login
</td>
</tr>
<tr>
<td>
Username
</td>
<td>
<input type='text' name='userid' value='' size='12' />
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<input type='password' name='password' size='12' />
</td>
</tr>
<tr>
<td>
<input type='hidden' name='formtype' value='login' />
</td>
</tr>
<tr>
<td>
<input type='submit' value='Submit' />
</td>
</tr>
</table></form>
Here is the code that is processing it in PHP...
foreach ($_POST as $var => $value) {
echo "$var = $value<br>";
}
I am using PHP 5 and Apache 2.2 on my server.
Any ideas?
EDIT...
I have narrowed it down to this...
$command = $_POST['formtype'];
When I removed the # sign from my $_POST, I am getting the following error...
Notice: Undefined variable: formtype in C:\webroot\utils\login.php on line 17
If I comment out that line, the POST data is passed into the program without a problem.
I would suggest changing the code you are using to display the contents of $_POST to a single call:
print_r($_POST);
Anytime you are displaying the entire contents of an array, this is better than a loop w/ echo, as it will show every value at every level of the array.
Also, as was mentioned in a comment, make sure you close the form in the html.
You never closed your <form> tag.
And I see now that someone beat me to it by a mile in the comments. Still, this is the right answer.
Have you tried taking the hidden input out of the table and placing it right after the opening form tag?
You can also use:
var_dump($_POST);
...to view the post variables.
Also, if any inputs are being dynamically created or might be missing from the POST variables... you can use:
variable = 'default';
if(isset($_Post['variable'])) $variable = $_POST['variable'];
...to dynamically set variables that could be there or not.
I changed my form to work with Twig. The changed form was not sending the hidden input value with post.
In case someone has the same problem, I solved it by doing the following.
The original line was:
<input hidden name='foo[{{ loop.index }}][id]' value='{{id}}' />
I sold it by making type='hidden':
<input type='hidden' name='foo[{{ loop.index }}][id]' value='{{id}}' />
Please try with:
<form action="..." method="post" enctype="application/x-www-form-urlencoded">
dear all..i have a textfield
<tr>
<td>
<td><input type="text" id="model_name"></td>
</td>
</tr>
and a cell
<tr>
<td><div id="value">//i want data show here after fill textfield</div>
</td>
</tr>
beside that, i've a table "settingdata" in database it consist of 2 field:itemdata and remark..
itemdata's value are "UD" and remark's value are "FM=87.5-108.0MHZ"...
what must i do if i want after type model name "car01UD" at textfield inside
<div id="value"></div>
can show "FM=87.5-108.0mhz"...
if you want "0103" + value of itemdata then do following
<input type="text" id="mod" value="0103<?PHP echo $itemdata ?>">
Looks like you are trying to do basic Ajax functionality. Consider reading some Ajax tutorials to get an idea how to do this.