I got stuck here with the codeigniter form validation. Here is my code below.
$match_data = $this->input->post('match_data');
$this->load->library('form_validation');
foreach($match_data as $roundno => $round_data) {
foreach ($round_data as $roundtimes => $round_times_data) {
foreach ($round_times_data as $match_no => $match) {
foreach ($match as $games_times => $games) {
$this->form_validation->set_rules("match_data[".$roundno."][".$roundtimes."][".$match_no."][".$games_times."][score1]", "score1", "trim|xss_clean|numeric");
$this->form_validation->set_rules("match_data[".$roundno."][".$roundtimes."][".$match_no."][".$games_times."][score2]", "score2", "trim|xss_clean|numeric");
}
}
}
}
When i am submitting a,b,c or anything like this the validation check is fine. but once i put proper numeric value it still showing me error. Please help me to find out the issue? Here is my input filed code
<div class="row">
<div class="col-sm-6">
<input class="form-control" type="text" name="match_data[<?php echo $edit_round_num; ?>][<?php echo $matchtime; ?>][<?php echo $match; ?>][<?php echo $gametime+1; ?>][score1]" value="<?php echo $games_per_match_array_one[$gametime]; ?>" />
</div>
<div class="col-sm-6">
<input class="form-control" type="text" name="match_data[<?php echo $edit_round_num; ?>][<?php echo $matchtime; ?>][<?php echo $match; ?>][<?php echo $gametime+1; ?>][score2]" value="<?php echo $games_per_match_array_two[$gametime]; ?>" />
</div>
</div>
In case of validating numeric values validation trim|xss_clean doesn't work or needed. So once removed trim|xss_clean the code starts working.
Related
How to get value post array in codeigniter?
I have problem when I get value post array and echo the value. How to show post value when submit?
here the error message:
A PHP Error was encountered
Severity: Notice
Message: Uninitialized string offset: 0
Filename: controllers/blablabla
view html:
<?php $i=0; foreach ($doc as $row) { ?>
<label>
<input name="size[<?php echo $i; ?>]" type="checkbox" value="<?php echo $row['doc']; ?>"> <?php echo $row['doc']; ?>
</label>
<?php $i++; } ?>
controller :
$size = $this->input->post('size');
for ($i=0; $i<count($doc); $i++)
{
echo $size[$i];
}
Change the way name of checkbox written as follows,
<?php foreach ($doc as $row) { ?>
<label>
<input name="size[]" type="checkbox" value="<?php echo $row['doc']; ?
>"> <?php echo $row['doc']; ?>
</label>
<?php } ?>
And in post method,
$size_arr = $this->input->post('size');
foreach($size_arr as $v){
echo $v;
}
if for some reason it is not working then check with,
$size_arr = $_POST['size'];
foreach($size_arr as $v){
echo $v;
}
EDIT
One more alternative,
$arr = $this->input->post();
$size_arr = $arr['size'];
foreach($size_arr as $v){
echo $v;
}
Core version,
$arr = $_POST;
$size_arr = $arr['size'];
foreach($size_arr as $v){
echo $v;
}
Your html form code should be like below.
<input name="size[<?php echo $i; ?>]" type="checkbox" value="<?php echo $row['doc']; ?>">
Inside controller your code should be like below.
$size = $this->input->post('size');
foreach($size as $sa)
{
echo $sa;
}
No need to use $i in checkbox name in view file just take an array
View file
<?php foreach ($doc as $row) { ?>
<label>
<input name="size[]" type="checkbox" value="<?php echo $row['doc']; ?>"> <?php echo $row['doc']; ?>
</label>
<?php } ?>
Controller
$countsize = count($this->input->post('size'));
for ($i=0; $i<$countsize ; $i++)
{
echo $this->input->post('size')[$i];
}
This one works for me
In View file
<div id="area_input">
<div id="inputan" class="form-inline">
<div class="form-group col-sm-6">
<input type="text" class="form-control" name="size[]" placeholder="ukuran">
</div>
<div class="form-group col-sm-6">
<input type="text" class="form-control" name="size[]" placeholder="ukuran">
</div>
<div class="form-group col-sm-6">
<input type="text" class="form-control" name="size[]" placeholder="ukuran">
</div>
<div class="form-group col-sm-6">
<input type="text" class="form-control" name="size[]" placeholder="ukuran">
</div>
</div>
</div>
you can repeat input as needed.
in Controller file
$data = array(
'size' => $this->input->post('size'),
);
You can check stucture of array using print_r($data), or print 'em using:
foreach ($data as $key => $value) {
foreach ($value as $detail) {
echo $detail;
echo "<br>";
}
}
I am trying to use PHP to update an SQL table using HTML forms.
I want the user to be able to search for a VCR name and display it details in a while loop and then an update form will appear for the user to change its details in the database.
However every time i press the update button on the update form, the variables that hold the new details empty and become undefined.
<?php require('connect.php'); ?>
<?php require('headerPrivate.php'); ?>
<?php require('session.php');?>
<?php
//SEARCH PHP CODE
//THIS WORKS FINE AND ALL THE DETAILS APPEAR
if(isset($_POST["search"]))
{
//CREATE VARIABLES
$username=$_SESSION['username'];
echo "username: ".$username;
echo '<br>';
$vcrName=$_POST['name'];
echo "VCR Name: ".$vcrName;
echo '<br>';
echo '<br>';
//SELECT * FROM PRODUCT
$sql="SELECT *
FROM product
INNER JOIN user
ON product.owner_ID=user.user_ID
WHERE username='$username' AND name='$vcrName'";
echo "SQL SELECT 1: ".$sql;
echo '<br>';
echo '<br>';
//$vcrName=$_POST['name'];
$result = mysqli_query($con,$sql);
echo '<div class="row">';
echo '<div class="col-xs-6 col-md-4">';
while ($row_all = mysqli_fetch_assoc($result))
{
echo '<form method="post">';
echo "<u>Title: ".$row_all["name"].'</u>';
echo '<br>';
echo '<small>';
echo " Price: ".$row_all["price"];
echo '</small>';
echo '<br>';
echo "<p><u>Short Description:</u> ".$row_all["short_descripton"]."</p>";
echo '<br>';
echo "<p><u>Long Description:</u> ".$row_all["long_description"]."</p>";
echo '<br>';
echo '<hr>';
echo '</form>';
echo '<div>';
}
echo '</div>';
}
?>
<content>
<!--SEARCH FOR VCR NAME-->
<form class="form" method="post">
<label for="name" class="sr-only">VCR Name</label>
<input type="text" name="name" class="form-control" placeholder="VCR Name" required="" autofocus="" autocomplete="off">
<button name="search" type="search" class="btn btn-success btn-block">Search</button>
</form>
<?php
//This is where i run into issues. The old name in the variable $vcrName is empty and i need it for the update SQL statement.
//UPDATE PHP
if(isset($_POST["alter"]))
{
//CREATE A SESSION VARIABLE FOR THE CUSTOMER ID
$customer_ID=$_SESSION['customer_ID'];
echo "Customer ID: ".$customer_ID;
echo '<br>';
//CREATE VARIABLES
$changeTitle=$_POST["titleChange"];
$changesDescChange=$_POST["sDescChange"];
$changelDescChange=$_POST["lDescChange"];
$changepriceChange=$_POST["priceChange"];
$vcrName=$_POST['name'];
//UPDATE SQL
$sql_update="UPDATE product
SET
name='$changeTitle',
short_descripton='$changesDescChange',
long_description='$changelDescChange',
price='$changepriceChange'
WHERE
owner_ID='$customer_ID' AND name='$vcrName'";
echo "SQL Update 0: ".$sql_update;
echo '<br>';
echo '<br>';
echo "Updated Name: ".$changeTitle;
echo '<br>';
echo '<br>';
echo "SQL Update 1: ".$sql_update;
return $sql_update;
echo '<br>';
echo '<br>';
$result_update = mysqli_query($con,$sql_update);
if($result_update){
echo "Update Successful!";
}
else {
echo "Update Unsuccessful";
}
}
?>
<!--UPDATE FORM-->
<form class="form" method="post">
<label for="titleChange" class="sr-only">VCR Name</label>
<input type="text" name="titleChange" class="form-control" placeholder="VCR Name" required="" autofocus="" autocomplete="off">
<label for="sDescChange" class="sr-only">Short Description</label>
<input type="text" name="sDescChange" class="form-control" placeholder="Short Description" required="" autofocus="" autocomplete="off">
<label for="lDescChange" class="sr-only">Long Description</label>
<input type="text" name="lDescChange" class="form-control" placeholder="Long Description" required="" autofocus="" autocomplete="off">
<label for="priceChange" class="sr-only">Price</label>
<input type="text" name="priceChange" class="form-control" placeholder="Price" required="" autofocus="" autocomplete="off">
<button name="alter" type="submit">Change</button>
</form>
</content>
</body>
</html>
Your PHP code refers to a POST variable that doesn't exist:
$vcrName=$_POST['name'];
In your update form, you need to pass it as a hidden value:
<input type="hidden" name="name" value="<?=htmlspecialchars($vcrName)?>"/>
It's not clear if these are two separate PHP scripts (or if so, why they are) so you may need to put in a database call to get that value. From a user interface point of view, one is typically given the existing values when updating a record anyway. This would mean getting the data and giving each form element a value attribute.
I managed to achieve specific database array order, now I'd like to print it in proper combination, but I have no idea how the 'foreach' loop should look like.
What I have now is:
$textfields = get_settings('test_testbutton');
if (!empty($textfields)) {
foreach ($textfields as $textfield) {
?>
<p>
<input type="text" id="<?php echo $value['id']; ?>" name="test_testbutton[0][]" value="<?php echo $textfield; ?>" placeholder="Input Value"/>
<input type="text" id="<?php echo $value['id']; ?>" name="test_testbutton[1][]" value="<?php echo $textfield; ?>" placeholder="Input Value"/>
Remove
</p>
<?php
}
} else {
}
I really don't know how to change 'echo $textfield' and make it work. I tried adding [] to '$textfields' value and then echo $textfield[] or $textfield[0], but with no success :(
I attached a .jpg file to make it more understandable.
attachment
After your update. It seems that you forget a foreach. I hope it's helpful to achieve what you want.
// Following your dump
$textfields = array( 0 => array( 1234, "qwer", "abcd"), 1 => array("5678", "tyui", "efgh"));
if (!empty($textfields)) {
foreach ($textfields as $textfield) {
// First loop : 0 => array( 1234, "qwer", "abcd")
// Second loop: 1 => array("5678", "tyui", "efgh")
foreach ($textfield as $oneValue) {
// Loop on the second array $textfield
}
}
The Text in the attached image is a serialized array, so if you want to use foreach on this text you have to unserialize it, then an array will be created
$arr = unserialize($textfields);
I think this will help you and it will work :)
$textfields_data = get_settings('test_testbutton');
if (!empty($textfields_data))
{
$textfields = unserialize($textfields_data);
$i = 0;
foreach ($textfields as $textfield) {
?>
<p>
<input type="text" id="" name="test_testbutton1_<?php echo $i; ?>" value="<?php echo $textfield[$i]; ?>" placeholder="Input Value"/>
<input type="text" id="" name="test_testbutton2_<?php echo $i; ?>" value="<?php echo $textfield[$i]; ?>" placeholder="Input Value"/>
Remove
</p>
<?php
$i++;
}
}
else
{
}
I have set up a group of checkboxes. They are dynamic, so the number of checkboxes will be different dependent on the person using the site. The structure of how the checkboxes are created is:
<label for="plabackformat-holder-label">Format</label>
<div class=" playbackformat-holder-<?php echo $num; ?> playbackformat-holder">
<div class="playback-format-radio-buttons">
<label for="notset-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="notset" name="playback_format[<?php echo $second_num; ?>]" id="notset-<?php echo $num; ?>" <?php if($field['playback_format'] == 'notset') { echo 'checked'; } ?>>None
</label>
<label for="dvd-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="dvd" name="playback_format[<?php echo $second_num; ?>]" id="dvd-<?php echo $num; ?>" <?php if($field['playback_format'] == 'dvd') { echo 'checked'; } ?>>DVD
</label>
<label for="bluray-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="bluray" name="playback_format[<?php echo $second_num; ?>]" id="bluray-<?php echo $num; ?>" <?php if($field['playback_format'] == 'bluray') { echo 'checked'; } ?>>Bluray
</label>
<label for="3d-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="3d" name="playback_format[<?php echo $second_num; ?>]" id="3d-<?php echo $num; ?>" <?php if($field['playback_format'] == '3d') { echo 'checked'; } ?>>3d
</label><br />
</div>
</div>
My save function is:
$new = array();
for ( $i = 0; $i < $count; $i++ ) {
$new[$i]['playback_format'] = $playbackFormats[$i];
}
I've been reading up on this issue and it seems its because my input fields do not contain unique names. I'm trying to store the data into an array, so it would be ['playback_format'] => dvd,3d,bluray or something similar.
Right now its only storing the last checked value. Is there a way I can use a forloop or something to iterate over the checked values and push them into my array??
You can just get rid of the "$second_num" in each <input name="playback_format[]"/> html tag. This will put everything into an array for you once you submit the form. You can check this by adding this line to the page as a test.
<?php print_r($_REQUEST['playback_format']); ?>
Generally, You want to avoid any loop if they aren't required.
Hope that helps with what you are doing.
What is $second_num? Does it need to be a part of the input name?
You can get PHP to recognise the submitted values as an array if you do it this way:
<input name="playback_format[<?php echo $second_num; ?>][]">
Or if you don't need $second_num as part of the name, just:
<input name="playback_format[]">
$_POST['playback_format'] will then be an array containing all the selected options.
There is a section in the PHP docs specifically about this behaviour.
Checkboxes have all the same name in your example. Name it differently like :
<label for="plabackformat-holder-label">Format</label>
<div class=" playbackformat-holder-<?php echo $num; ?> playbackformat-holder">
<div class="playback-format-radio-buttons">
<label for="notset-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="notset" name="playback_format_notset" id="notset-<?php echo $num; ?>" <?php if($field['playback_format_notset'] == 'notset') { echo 'checked'; } ?>>None
</label>
<label for="dvd-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="dvd" name="playback_format_dvd" id="dvd-<?php echo $num; ?>" <?php if($field['playback_format_dvd'] == 'dvd') { echo 'checked'; } ?>>DVD
</label>
<label for="bluray-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="bluray" name="playback_format_bluray" id="bluray-<?php echo $num; ?>" <?php if($field['playback_format_bluray'] == 'bluray') { echo 'checked'; } ?>>Bluray
</label>
<label for="3d-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="3d" name="playback_format_3d" id="3d-<?php echo $num; ?>" <?php if($field['playback_format_3d'] == '3d') { echo 'checked'; } ?>>3d
</label><br />
</div>
</div>
And Try this in PHP :
//WHen you want to see what is send in Post :
var_dump($_POST);
//So, for get result :
$tab = array();
foreach($_POST as $key =>$value){
$tab[$key] = $value;
//Display it
echo $key . "=" . $value;
}
how to display the result after submit the form
i want a display result after submit the form for print
example 1st im filling the form submit the result after the submit i want a screen to display same result
http://www.tizag.com/phpT/examples/formexample.php
how can i do this
please help me to fix this issue.
php form code
<?php
function renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error)
{
?>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div>
<p><span class="style9"><strong>G.R.N No:</strong></span><strong> *</strong>
<input name="grn" type="text" id="grn" value="<?php echo $grn; ?>" size="50" />
</p>
<p><span class="style9"><strong>Name:</strong></span><strong> *</strong>
<input name="name" type="text" id="name" value="<?php echo $name; ?>" size="50" />
</p>
<p><span class="style9"><strong>Roll No :</strong></span><strong> *</strong>
<input name="rollno" type="text" id="rollno" value="<?php echo $rollno; ?>" size="50" />
</p>
<p><span class="style9"><strong>Class:</strong></span><strong> *</strong>
<input name="class" type="text" id="class" value="<?php echo $class; ?>" size="50" />
</p>
<p><span class="style9"><strong>Fees Date :</strong></span><strong> *</strong>
<input id="fullDate" name="date" type="text" value="<?php echo $date; ?>" size="50" />
</p>
<p><span class="style9"><strong>Fees :</strong></span><strong> *</strong>
<input name="fees" type="text" value="<?php echo $fees; ?>" size="50" />
</p>
<span class="style9"><strong>Reference</strong></span><strong> *</strong>
<input name="reference" type="text" value="<?php echo $reference; ?>" size="50">
<br/>
<p class="style1">* required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
<?php
}
include('connect-db.php');
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$grn = mysql_real_escape_string(htmlspecialchars($_POST['grn']));
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$rollno = mysql_real_escape_string(htmlspecialchars($_POST['rollno']));
$class = mysql_real_escape_string(htmlspecialchars($_POST['class']));
$fees = mysql_real_escape_string(htmlspecialchars($_POST['fees']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$reference = mysql_real_escape_string(htmlspecialchars($_POST['reference']));
// check to make sure both fields are entered
if ($grn == '' || $name == '' || $rollno == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error);
}
else
{
// save the data to the database
mysql_query("INSERT fees SET grn='$grn', name='$name', rollno='$rollno', class='$class', fees='$fees', date='$date', reference='$reference'")
or die(mysql_error());
echo "<center>KeyWord Submitted!</center>";
// once saved, redirect back to the view page
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','','','','');
}
?>
Not quite shure what you are asking.
do you need help displaying the submited form data? or more spesific display for print?
to display it you would just need to make a html page that displays it.
like:
echo '<table><tr>';
echo '<td>';
echo '<Strong>Name:</strong><br/>';
echo $name;
echo '</td>';
echo '</tr></table>';
To display just the result and not the form, when you post to the same page you need to encapsulate the for code with an if statement.
if(isset($_POST['submit'])) {
//code for the php form
}else {
//code to display form
}
Use a new page for the "action" part in the form. On that new page, simply echo the $_POST of values you want to display. If you plan on making some sort of page so that people can check their entries, you could store these $_POST-data in sessions.
Simply call function on the bottom to disply posted values :
function showFormValues()
{
?>
<div>
<p><span class="style9"><strong>G.R.N No:</strong></span><strong> *</strong>
<?php echo $_POST['grn']; ?>
</p>
<p><span class="style9"><strong>Name:</strong></span><strong> *</strong>
<?php echo $_POST['name'];
</p>
and so on.
.
.
.
.
</div>
<?php
}
?>