This request is from a non programmer. An Osclass item post php form code sample is given below.
<?php
$action = 'item_add_post';
$prepare['s_phone_1'] = del_get_session('sPhone1') <> '' ? del_get_session('sPhone1') : #$item_extra['s_phone_1'];
?>
<div class="input-box">
<input type="tel" id="sPhone" name="sPhone1[]" value="" />
</div>
<div class="input-box">
<input type="tel" id="sPhone" name="sPhone1[]" value="" />
</div>
<div class="input-box">
<input type="tel" id="sPhone" name="sPhone1[]" value="" />
</div>
<?php
$prepare = $_POST['sPhone1'];
?>
<input type="hidden" name="sPhone1" value="<?php echo htmlentities(serialize($prepare['s_phone_1']));?>">
functions.php page has below code also.
function del_update_fields($item) {
if(!isset($item['pk_i_id']) || $item['pk_i_id'] <= 0) {
return false;
}
if(Params::existParam('sSold')) {
$fields = array(
's_phone' => (Params::getParam('contactPhone') <> '' ? Params::getParam('contactPhone') : Params::getParam('sPhone')),
's_phone_1' => Params::getParam('sPhone1'),
'i_condition' => Params::getParam('sCondition'),
'i_negotiable' => Params::getParam('sNegotiable'),
'i_transaction' => Params::getParam('sTransaction'),
'i_sold' => (Params::getParam('sSold') == 'on' ? 1 : Params::getParam('sSold'))
);
} else {
$fields = array(
's_phone' => (Params::getParam('contactPhone') <> '' ? Params::getParam('contactPhone') : Params::getParam('sPhone')),
's_phone_1' => Params::getParam('sPhone1'),
'i_condition' => Params::getParam('sCondition'),
'i_negotiable' => Params::getParam('sNegotiable'),
'i_transaction' => Params::getParam('sTransaction')
);
}
Item::newInstance()->dao->update(DB_TABLE_PREFIX.'t_item_delta', $fields, array('fk_i_item_id' => $item['pk_i_id']));
}
Screenshot of the db table
Requirement is to store phone numbers in above 3 name="sPhone1[]" inputs in "s_phone_1" as below example.
0091222222222;0091333333333;0091666666666;
But it stores N; in mysql. Need your help to correct above code to pass these numbers as an array. And later, need to get them back in to 3 lines as below in another page.
Phone 1: 0091222222222
Phone 2: 0091333333333
Phone 3: 0091666666666
Thanks in advance..
you are getting values in the array and now these array values you want in "s_phone_1"
use this code to get value in "s_phone_1"
here you can store $phonenumbers in your database
$phonenumbers = implode(";",$_POST['sPhone1']);
//echo $phonenumbers;
output
09874563214;09874563214;09874563214
now for getting value as per your requirement
get value from the database and store it in $phonenumbers
$getPhoneNumbers = explode(";",$phonenumbers);
echo "Phone 1:" .$getPhoneNumbers[0]."<br>";
echo "Phone 2:" .$getPhoneNumbers[1]."<br>";
echo "Phone 3:" .$getPhoneNumbers[2]."<br>";
output
Phone 1:09874563214
Phone 2:09874563214
Phone 3:09874563214
Related
hope you can help me on this..
i have this simple code :
foreach ($by_sellers as $seller_id => $info) {
print_r($info);
echo 'AMOUNT : ' . $info['total'] .'; STORE : '. $info['name'];
}
and the output is this :
Array(
['total'] => 100
['name'] => 'Store Name'
)
AMOUNT : ; STORE :
its really weird coz i cant read the values of the variable if done individually but it display on var_dump & print_r.. those data comes from input forms..
<input name="by_sellers[1]['total']" value="100">
<input name="by_sellers[1]['name']" value="Store Name">
hope someone find the light on this problem.. thanx..
found the solution and its a simple one..
i just edit the html code from this
<input name="by_sellers[1]['total']" value="100">
<input name="by_sellers[1]['name']" value="Store Name">
to this (removing the quotes of the string name)
<input name="by_sellers[1][total]" value="100">
<input name="by_sellers[1][name]" value="Store Name">
i'm doing this exercise to understand te concepts and syntax of php.The error i get is that the array is not being appended by a new entry from a text box, if i hard code the values the program works fine and prints the array values. but as soon as i put the variable it just assign a new value to the [0] index.
here is the code:
<label for="Name">Student Name:
<input type="text" name="StdName" placeholder="Your name">
</label>
<label for="Name">Grade:
<input type="text" name="StdGrade" placeholder="Your Grade">
</label>
<input type="submit" name="submit"value="submit">
</form>
<?php
// Associative array new
$studentName = $_POST['StdName'];
$studentGrade = $_POST['StdGrade'];
$classGrades = Array();
$classGrades['name'][] = $studentName;
$classGrades['grade'][] = $studentGrade;
echo "<br> <br> Your name is: $studentName and your grade is: $studentGrade <br> <br>";
foreach($classGrades as $key=>$value){
echo join($value,' <br>');
}
?>```
Your loop is wrong. You have separate name and grade arrays, so that's what you need to loop over:
foreach ($classGrades['name'] as $index => $name) {
$grade = $classGrades['name'][$index];
echo "$name<br>$grade<br>";
}
But it would be better if you didn't create separate array and kept the name and grade together in a single associative array:
$classGrades[] = ['name' => $studentName, 'grade' => $studentGrade];
Then your loop would look like:
foreach ($classGrades as $grade) {
echo $grade['name'] . "<br>" . $grade['grade'] . "<br>";
}
If you want the variable to persist between form submissions, you need to use a session variable.
Put
<?php
session_start();
at the beginning of the script. Then use $_SESSION['classGrades'] instead of $classGrades, and initialize it like this:
if (!isset($_SESSION['classGrades'])) {
$_SESSION['classGrades'] = array();
}
Basically you need to store your values, in some database.
Here is a simple start using a json file named students.json as database. That file will be created along your php file in the same folder, if not existing.
First you was missing the form elements and the method (by default it uses GET, thus this was not working).
Testing with isset() avoids populating the database with empty values, at page load or refreshs.
This is not very secure as such, you will have to work on that later on. Hint: What if a user insert a quote ' or " in his name?
<form method="post">
<label for="Name">Student Name:
<input type="text" name="StdName" placeholder="Your name">
</label>
<label for="Name">Grade:
<input type="text" name="StdGrade" placeholder="Your Grade">
</label>
<input type="submit" name="submit"value="submit">
</form>
<?php
if (!is_file("students.json")){
file_put_contents("students.json", json_encode([]) );
}
$classGrades = json_decode(file_get_contents("students.json"),true);
if (isset($_POST['StdName']) && isset($_POST['StdGrade'])){
var_dump($classGrades);
$studentName = $_POST['StdName'];
$studentGrade = $_POST['StdGrade'];
$classGrades['name'][] = $studentName;
$classGrades['grade'][] = $studentGrade;
echo "<br> <br> Your name is: $studentName and your grade is: $studentGrade <br> <br>";
file_put_contents("students.json",json_encode($classGrades));
}
echo "<table><tr><th>Name</th><th>Grade</th></tr>";
for ($i = 0;$i < count($classGrades['name']);$i++){
echo "<tr><td>".$classGrades['name'][$i] . "</td><td> " .$classGrades['grade'][$i] . "</td></tr>";
}
echo "</table>";
I have 10 rows of following code in my purchase order form. Where user add value manually
<input type="text" name="name[]" />
<input type="text" name="code[]" />
<input type="text" name="rate[]" />
<input type="text" name="tax[]" />
<input type="text" name="amount[]" />
In some cases if the purchase order is only for 3 items then the rest of 7 rows will be blank. So how can I create an Insert query to add only filled cell value in Product_purchased table according to the values of their respective rows.
$name = $_POST['name'];
$code = $_POST['code'];
foreach( $name as $names ) {
print $names ;
}
The above code will combine all the names but I want value of a rows in it, an array like
array ("name" => Product 1, "code" => P30, "rate" => 1000, "tax" => 12, "amount" => 1120)
and then submit it in an Insert query.
$output = [];
for($i=0;$i<count($name);$i++){
$data['name'] = $name[$i];
$data['code'] = $code[$i];
...
$output[] = $data;
}
Create a array and iterate all arrays and push.
$output will have multi array of all products. looping $output to perform insert to database.
I'm stuck on this and I'm not actually seeing where exactly the problem is.
I have a bunch of input tags which are placed like that:
<div class="col-md-3">
<label>Material 1</label>
<input hidden="hidden" name="idMaterial[]" value="13" type="text" />
<input class="form-control" name="total[]" type="text" />
</div>
<div class="col-md-3">
<label>Material 2</label>
<input hidden="hidden" name="idMaterial[]" value="8" type="text" />
<input class="form-control" name="total[]" type="text" />
</div>
I have 20 input like that, the idea here is that if I write 10 in the Material 1's input field, my DB would receice something like:
id => A.I
idMaterial => 13
total => 10
But once I run the code, it comes like that (repeating the idMaterial, even if I write in more than 1 input. It always repeat the first):
idMaterial: 13
total: 10
idMaterial: 13
total: 20
That's the code I'm using to receive that form:
$idMaterial = array();
$total = array();
if($this->input->post('total')){
foreach($this->input->post('idMaterial') as $row){
$idMaterial = $row;
foreach($this->input->post('total') as $row2){
$total = $row2;
echo 'Material: '. $idMaterial .'<br> Total: '. $total. '<br><br>'; // TESTING THE OUTPUT
$query = $this->pedido->salvaLabMaterial($total, $idMaterial); // I'M SENDING THE DATA TO MODEL HERE
}
}
}
Any tips is very welcome.
#RiggsFolly
That's where I'm sending the posts the way it comes from the HTML form as you said:
$query = $this->pedido->salvaLabMaterial($this->input->post('total'),
$this->input->post('idMaterial'),
);
The model:
public function salvaLabMaterial($this->input->post('idMaterial'), $this->input->post('total')){
$query = $this->db->query(" insert into labpedidomaterial (idMaterial, total) values ('".$this->input->post('idMaterial')."', '".$this->input->post('total')."') ");
}
I'm searching a way to create arrays from a form and transfer it to another page:
This is my actual code :
<?php
$nba = $_GET["nbadultes"]; // recup number of passangers
$adu = array();
for ($i=1;$i<=$nba;$i++)
{
echo '<h3>Passager '.$i.'</h3>
<label>CIVILITÉ</label>
<select class="full-width" name="Civilite">
<option value="Mr" selected >Mr.</option>
<option value="Mrs" >Mrs.</option>
</select>
<label>FIRSTNAME *</label>
<input type="text" name="FIRSTNAME"/>
<label>LASTNAME *</label>
<input type="text" name="LASTNAME"/> ';
$adu[$i][civilite] = $_POST['civilite'];
$adu[$i][FIRSTNAME] = $_POST['FIRSTNAME'];
$adu[$i][LASTNAME] = $_POST['LASTNAME'];
}
$_SESSION['liste_adultes'] =$adu;
?>
And this code in the next page to read the array :
<?php
if (isset($_SESSION['liste_adultes']))
print_r ($liste_adultes);
?>
But I don't know why the array is empty :
Array
(
[1] => Array
(
[Civilite] =>
[FIRSTNAME ] =>
[LASTNAME] =>
)
[2] => Array
(
[Civilite] =>
[FIRSTNAME ] =>
[LASTNAME] =>
)
)
You need to take care of the form inputs you send
As I can see, right now the form is not submitted, so you don't need a session but a form which have set the action to a page that handles the sent data. Something like:
<form method="post" action="my-action-file.php">
<?php
$nba = $_GET["nbadultes"];
for ($i = 1; $i <= $nba; $i++) {
echo '<h3>Passager '.$i.'</h3>
<label>CIVILITÉ</label>
<select class="full-width" name="Civilite[]">
<option value="Mr" selected >Mr.</option>
<option value="Mrs" >Mrs.</option>
</select>
<label>FIRSTNAME *</label>
<input type="text" name="FIRSTNAME[]"/>
<label>LASTNAME *</label>
<input type="text" name="LASTNAME[]"/> ';
}
?>
<input type="submit" value="Submit" />
</form>
In the my-action-file.php file you can get the $_POST variables as:
<?php
foreach ($_POST['Civilite'] as $key => $civilite) {
$firstName = $_POST['FIRSTNAME'][$key];
$lastName = $_POST['LASTNAME'][$key];
// now you have the sent data.. just use it like you need..
}
// of course you can add these data to an array if you like so
In the second page you need to read the session data, then read the specific value you saved in the session into the variable you used:
<?php
if(!isset($_SESSION)){ session_start();}
if(isset($_SESSION['liste_adultes'])){
$liste_adultes = $_SESSION['liste_adultes'];
print_r($liste_adultes);
}
?>
Also, in the first page, make sure you post data and read it with $_POST; or via query string with $_GET