How to get array data from HTML form? - php

I have a form in HTML, and it stores data into array structure.
echo '<input type="text" style="text-align:right;" size="5" name="row[1][kidname]" />';
echo '<input type="text" type="number" style="text-align:right;" size="5" name="row[1][kidweight]" />';
echo '<input type="text" type="number" style="text-align:right;" size="5" name="row[1][kidheight]" />';
echo '<input type="text" style="text-align:right;" size="5" name="row[2][kidname]" />';
echo '<input type="text" type="number" style="text-align:right;" size="5" name="row[2][kidweight]" />';
echo '<input type="text" type="number" style="text-align:right;" size="5" name="row[2][kidheight]" />';
Supposed I need to get the second kid's weight value, how should I do? I have tried:
$_POST[row[2][kidweight];
$_POST['row[2][kidweight'];

You can try also.
$_GET['row'][2]['kidweight'];

Related

how to run a form with parameter in codeigniter?

I am new to codeigniter and i want to run my sms web service code in view when user insert number.
but the post method is empty! can anyone help me?
var_dump($_POST['sendSms']);exit;
if(array_key_exists('sendSms', $_POST)) {
sendSms();
}
function sendSms(){ //my sms web service code}
echo '<form method="Post" action= "http://crm.oynarco.ir/admin/settings?group=sms">';
?>
<input type="hidden"
name="<?php echo $this->security->get_csrf_token_name()?>"
value="<?php echo $this->security->get_csrf_hash()?>">
<?php
echo '<input type="hidden" name="username" value="09172030433"/><br />';
echo '<input type="hidden" name="password" value="Oynar1234" /><br />';
echo '<input type="text" name="to" class="form-control test-phone" placeholder="'._l('staff_add_edit_phonenumber').'" /><br />';
echo '<textarea type="text" name="text" class="form-control sms-gateway-test-message" placeholder="'._l('test_sms_message').'" ></textarea><br />';
echo '<input type="text" name="From" class="form-control" value="5000203069627" /><br />';
echo '<input type ="submit" name="sendSms" class="btn btn-info send-test-sms" value="'._l('send_test_sms').'" />';
echo '</form>';
in action root to controller then in controller get the post method and add your sms function.
?>
<form method="Post" action= "settings/sendSms">
<input type="hidden"
name="<?php echo $this->security->get_csrf_token_name()?>"
value="<?php echo $this->security->get_csrf_hash()?>">
<?php
echo '<input type="hidden" name="username" value = "09172030433"/><br />';
echo '<input type="hidden" name="password" value ="Oynar1234" /><br />';
echo '<input type="text" name="to" class="form-control test-phone" placeholder="'._l('staff_add_edit_phonenumber').'" /><br />';
echo '<textarea type="text" name="text" class="form-control sms-gateway-test-message" placeholder="'._l('test_sms_message').'" ></textarea><br />';
echo '<input type="text" name="From" class="form-control" value = "5000203069627" /><br />';
echo '<input type ="submit" name="sendSms" class="btn btn-info send-test-sms" value = "'._l('send_test_sms').'" />';
echo '</form>';
in controller:
public function sendSms(){
//var_dump($_POST);exit;
if (isset($_POST) ){
//send sms
}

How to update all data in html at once?

I'm creating a form in html, I can update per data, but I want to update all data by one click?
<?php foreach($queryRecords as $res) :?>
<tr>
<form action="" method="POST" name="form" id="form">
<td>
<input name="id" type="hidden" class="normalinput" id="id" value="<?php echo $res["id"];?>">
<input name="gp_name" type="hidden" class="normalinput" id="gp_name" value="<?php echo $res["gp_name"];?>">
<input name="date" type="hidden" class="normalinput" id="date" size="10" value="<?php echo $res["date"];?>">
<input name="day" type="hidden" class="normalinput" id="day" size="1" value="<?php echo $res['day'];?>">
<input name="starting_time" type="hidden" class="normalinput" id="starting_time" size="9" value="<?php echo $res["starting_time"];?>">
<input name="type" type="hidden" class="normalinput" id="type" size="4" value="<?php echo $res["type"];?>">
<input name="duration" type="hidden" class="normalinput" id="duration" size="4" value="<?php echo $res["duration"];?>">
<input name="checkin" type="hidden" class="normalinput" id="checkin" size="8" value="<?php echo $res["checkin"];?>">
<input name="checkout" type="hidden" class="normalinput" id="checkout" size="8" value="<?php echo $res["checkout"];?>">
<input name="country" type="hidden" class="normalinput" id="country" size="7" value="<?php echo $res["country"];?>">
<input name="city" type="hidden" class="normalinput" id="city" size="6" value="<?php echo $res["city"];?>">
<input name="supplier" type="hidden" class="normalinput" id="supplier" size="8" value="<?php echo $res["supplier"];?>">
<input name="arrange" type="hidden" class="normalinput" id="arrange" size="10" value="<?php echo $res["arrange"];?>">
<input name="no_of_day" type="hidden" class="normalinput" id="no_of_day" size="1" value="<?php echo $res["no_of_day"];?>">
<input name="qua" type="hidden" class="normalinput" id="qua" size="1" value="<?php echo $res["qua"];?>">
<input name="cost" type="hidden" class="normalinput" id="cost" size="6" value="<?php echo $res["cost"];?>">
<input name="profit_rate" type="hidden" class="normalinput" id="profit_rate" size="4" value="<?php echo $res["profit_rate"];?>">
<input name="currency_rate" type="hidden" class="normalinput" id="currency_rate" size="4" value="<?php echo $res["currency_rate"];?>">
<input name="eurbuy" type="hidden" class="normalinput" id="eurbuy" size="4" value="<?php echo $res["eurbuy"];?>">
<input name="vat" type="hidden" class="normalinput" id="vat" size="4" value="<?php echo $res["vat"];?>">
<input name="pprice" type="hidden" class="normalinput" id="pprice" size="4" value="<?php echo round( $res["cost"] * $res["profit_rate"] * $res["currency_rate"] / $res["eurbuy"],0) ;?>">
<input name="total" type="hidden" class="normalinput" id="total" value="<?php echo round( $res["pprice"] * $res["no_of_day"] * $res["qua"] ,0) ;?>">
<input name="reference" type="hidden" class="normalinput" id="reference" size="10" value="<?php echo $res['reference'];?>">
<input name="supplement_rate" type="hidden" class="normalinput" id="supplement_rate" size="5" value="<?php echo $res["supplement_rate"];?>">
<input name="supplement" type="hidden" class="normalinput" id="supplement" size="5" value="<?php echo round( $res["supplement_rate"] * $res["profit_rate"] * $res["no_of_day"] * $res["currency_rate"] / $res["eurbuy"],0) ;?>">
<button class="btn btn-info" type="button" onClick="update(form); window.location.reload();"><i class="fas fa-edit"></i></button>
<button class="btn btn-danger" type="button" onClick="window.location.href='gp_delete.php?id=<?php echo $res['id'];?>'"><i class="fas fa-trash-alt"></i></button>
</td>
</form>
</tr><?php endforeach;?>
since I need to update per data, how do you create one button to update all data for the table?
document.querySelector('form').addEventListener('submit', (e) => {
const formData = new FormData(e.target);
// Now you can use formData.get('foo'), for example.
// Don't forget e.preventDefault() if you want to stop normal form .submission
});
This is a nitpicky answer, but let me explain why this is a better solution:
We're properly handling a form submit rather than a button press. Some people like to push enter on fields. Some people use alternative input devices such as speech input or other accessibility devices. Handle the form submit and you correctly solve it for everyone.
We're digging into the form data for the actual form that was submitted. If you change your form selector later, you don't have to change the selectors for all the fields. Furthermore, you might have several forms with the same input names. No need to disambiguate with excessive IDs and what not, just track the inputs based on the form that was submitted. This also enables you to use a single event handler for multiple forms if that is appropriate for your situation.
The FormData interface is fairly new, but is well supported by browsers. It's a great way to build that data collection to get the real values of what's in the form. Without it, you're going to have to loop through all the elements (such as with form.elements) and figure out what's checked, what isn't, what the values are, etc. Totally possible if you need old browser support, but the FormData interface is simpler.
I'm using ES6 here... not a requirement by any means, so change it back to be ES5 compatible if you need old browser support.

Concatenate right an input

I am trying to concatenate this code but i can not do it right
echo '<input type="text" id="text" name="username"
value=".<?php if(isset($_COOKIE['.'rememberme'.']))
{echo $_COOKIE['.'remember me'.'];}?>."
placeholder="Username"/><br />';
You're already in PHP there...
<?php
echo '<input type="text" id="text" name="username"';
if( isset($_COOKIE['rememberme'])) echo ' value="'.htmlspecialchars($_COOKIE['rememberme']).'"';
echo ' placeholder="Username" /><br />';
printf('<input type="text" id="text" name="username" value="%s" placeholder="Username"/><br />',
(isset($_COOKIE['remember_me'])) ? htmlspecialchars($_COOKIE['remember_me']) : ''
);

how to post selected check box with each text box value

i want insert post text box values of each selected check boxes to the database.
<input type="checkbox" name="email[]" value="<?php echo $actions_to_be[$z]['student_email'] ?>" />
<input type="text" name="timetable_id[]" value="<?php echo $actions_to_be[$z]['timetable_id'] ?>" size="10px"/>
<input type="text" name="sid[]" value="<?php echo $actions_to_be[$z]['sid'] ?>" size="10px" />
<input type="checkbox" name="email[]" value="<?php echo $i; ?>" />
<input type="text" name="timetable_id[<?php echo $i; ?>]" value="<?php echo $actions_to_be[$z]['timetable_id'] ?>" size="10px"/>
<input type="text" name="sid[<?php echo $i; ?>]" value="<?php echo $actions_to_be[$z]['sid'] ?>" size="10px" />
$i should be increased for every box of course. Then you can simply take the ids from the $_POST['email'] array (possibly rename it to something more meaningful though) and retrieve the values in $_POST['timetable_id'][$id] and $_POST['sid'][$id].

Counting $_POST

I have a big form that contains X amount of posts that has 15 fields per post along with 1 hidden field.
Let's assume I have 14 posts. This means my form would send 211 fields (14x15 fields plus 1 hidden field).
The user does not have to fill in all fields.
I want to count the number of posts that the form sends but I seem to be running into difficulty.
Using count($_POST) returns 152. This leads me to believe that count() is ignoring empty fields.
As a result, using a formula such as (count($_POST) - 1) / 15 would return the wrong result (10.0666) and is inefficient should the number of fields change in the future.
So, does anyone have any ideas as to how to get the proper count of my posts?
My form looks like so:
<form name="scraped" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" method="post">
<input type="hidden" name="OSscraper_hidden" value="N">
<?php
$inpCnt = 0;
foreach($articles as $item) {
?>
<input type="text" name="title_<?php echo $inpCnt; ?>">
<input type="text" name="name_<?php echo $inpCnt; ?>">
<input type="text" name="url_<?php echo $inpCnt; ?>">
<input type="text" name="img_<?php echo $inpCnt; ?>">
<input type="text" name="pet_<?php echo $inpCnt; ?>">
<input type="text" name="color_<?php echo $inpCnt; ?>">
<input type="text" name="value_<?php echo $inpCnt; ?>">
<input type="text" name="height_<?php echo $inpCnt; ?>">
<input type="text" name="weight_<?php echo $inpCnt; ?>">
<input type="text" name="hair_<?php echo $inpCnt; ?>">
<input type="text" name="eyes_<?php echo $inpCnt; ?>">
<input type="text" name="race_<?php echo $inpCnt; ?>">
<input type="text" name="phone_<?php echo $inpCnt; ?>">
<input type="text" name="address_<?php echo $inpCnt; ?>">
<input type="text" name="zip_<?php echo $inpCnt; ?>">
<?php
$inpCnt++;
} ?>
<input type="submit" value="Submit">
</form>
Change your form to look like:
<input type="text" name="foo[<?php echo $inpCnt; ?>][title]">
<input type="text" name="foo[<?php echo $inpCnt; ?>][name]">
<input type="text" name="foo[<?php echo $inpCnt; ?>][url]">
Then you will get:
$_POST['foo'] = [
0 => ['title' => '...', 'name' => '...', 'url' => '...'],
1 => ...,
...
];
It saves you from having to do the grouping yourself, and is easier to count or iterate over the input.
why not just count($articles)*15 and echo into a hidden input. You are using another hidden input anyway....
Try this code, and demo is here
Please just use the idea not the exact copy.
<?php
error_reporting(E_ALL ^ E_NOTICE);
//debugging
if(#$_POST['submit'] == 'Submit'){
echo '<pre>';
print_r($_POST);
echo '</pre>';
echo "<br>\n";
echo 'Number of posts = count($_POST["posts"])='.count(#$_POST['posts'])."<br>\n";
//finding number of posts that are set and not empty
$count = 0;
foreach($_POST['posts'] as $v1){
//$v is an array
foreach($v1 as $v1k=> $v1v){
if(strlen($v1v) > 0){
++$count;
$inputs[$v1k] = $v1v;
}
}
}
echo 'Count of non-empty posts = $count = '.$count."<br>\n";
echo '<pre>';
print_r($inputs);
echo '</pre>';
}
?>
<form name="scraped" action="" method="post">
<input type="hidden" name="OSscraper_hidden" value="N">
<?php
$articles =array('test');
$inpCnt = 0;
foreach($articles as $item) {
?>
<input type="text" name="posts[][title_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][name_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][url_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][img_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][pet_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][color_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][value_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][height_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][weight_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][hair_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][eyes_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][race_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][phone_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][address_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][zip_<?php echo $inpCnt; ?>]">
<?php
$inpCnt++;
} ?>
<input type="submit" value="Submit" name="submit">
</form>

Categories