I have a form in my HTML file that allows the user to choose a date, this input binds to an Angular value. The value is then sent into a PHP file to be inserted into a database. Below is the code that shows how i currently have it set up.
HTML input form
<input class="input" ng-model="release" type="date" />
Angular controller
$http.post("php/addEpisode.php", {
'release': $scope.release})
PHP file
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
#$release = $request->release;
$query = "INSERT INTO episode ( release )
VALUES ( '{$release}' )";
$result = mysqli_query($connection, $query);
All the other values (which are not shown here) that are inserted into the table work fine. It is just this value that is an issue.
In the database the 'release' value is stored as a date with a format of 'yyyy-mm-dd'.
<---EDIT--->
I have found the reason it was not working. The name of the column in the table could not be set to 'release', it had to be something different as 'release' is a key word.
this work for me very well
in html
<div class="form-group col-md-6">
<label class="col-md-4 control-label">*Fecha Radicacion</label>
<div class="col-md-8 inputGroupContainer">
<div class="input-group">
<md-datepicker
ng-model="formulario.fechaRadicacion"
disabled
>
</md-datepicker>
</div>
</div>
</div>
in the controller
$scope.fechaRadicacion = new Date();
$scope.fechaActual=function(){
$scope.formulario.fechaRadicacion=$scope.fechaRadicacion.toISOString().slice(0, 10);
console.log("esta es la fecha en radicacion JS ",$scope.fechaRadicacion);
console.log("esta es a fecha en formulario.fechaRadicacion",$scope.formulario.fechaRadicacion);
}
in the PHP
//echo 'el dato es :<pre>'.$r->formulario->fechaRadicacion.;
Related
Hi I have a form to add members with their birth date. The input field for birth date is:
<div class="form-group">
<label for="data_nascita" class="col-sm-3 control-label">Birth Date</label>
<div class="col-sm-9">
<input name="data_nascita" type="date" class="form-control" id="data_nascita" placeholder="gg/mm/aaaa" />
</div>
</div>
Data are correctly uploaded to MYSQL and date format is
$data_nascita = ($_POST['data_nascita']);
var_dump($data_nascita); => string(10) "2003-04-15"
In the database is stored correctly, and it appears as
2018-12-14 18:50:48
I want to have the possibility to edit the information about the person (i.e. changing the birth date), and I created an edit file where all the database information is retrieved and appears in form fields that can be edited and updated in MYSQL.
Everything works fine except for dates, which appears as gg/mm/aaaa
The code I used for retrieving data is as usual:
<?php
$query = "SELECT data_nascita FROM volontari WHERE id_volontari = '$id'";
$res = $mysqli ->query($query);
$row = $res->fetch_array (MYSQLI_ASSOC);
$data_nascita = gira_data_db($row['data_nascita']);
function gira_data_db($data)
{
$array = explode( "-" , $data);
$array2 = array($array[2], $array[1], $array[0]);
$testo = implode( "/" , $array2);
return $testo;
}
?>
<div class="form-group">
<label for="data_nascita" class="col-sm-3 control-label">Birth Date</label>
<div class="col-sm-9">
<input name="data_nascita" type="date" class="form-control" id="data_nascita" value="<?php echo $data_nascita ?>" />
</div>
</div>
The date retrieved is
var_dump(gira_data_db($row['data_nascita']);) => string(10) "15/04/2003"
However in my form field the data appears as 00/00/0000. If I echo the date in an input field type=text, it appears correct.
Am I doing something wrong?
The problem is when you explode by '-' array[2] is '14 18:50:48' and it's not valid value for input with date type.
simply change gira_data_db function as follows:
function gira_data_db($data)
{
return date("YYYY-MM-DD", strtotime($data));
}
I hope it would help you
One my form I have the option to choose from a select box, or to manually enter the information.
I want to prevent user from entering both. They should either choose from the select OR enter manually if their selection is not already there.
html code
<div class="row">
<div class="form-group col-sm-4">
<div class="row">
<div class="col-sm-3">
<label for="selectComplvl" class="regions" >Region</label></div>
<div class="col-sm-8">
<select class="form-control regionbox" id="region" value="" name="region">
<option>---Select---</option>
<?php
$region_sql = "SELECT * FROM `regions`";
$region_res = mysqli_query($con, $region_sql);
if(mysqli_num_rows($region_res) > 0){
while($region_row = mysqli_fetch_array($region_res)){
echo "<option value='".$region_row[id]."'>".$region_row[region]."</option>";
}
}
?>
</select>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-group col-sm-4">
<div class="row">
<div class="col-sm-4">
<label for="feisEntered" class="feis">Feis Entered</label></div>
<div class="col-sm-8">
<select class="form-control feisbox" id="feisEntered" name="feisEntered">
<option>First choose a Region</option>
</select>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-group col-sm-4">
<div class="row">
<div class="col-sm-3">
<label for="feisEntered" class="enterfeis">Or Enter</label></div>
<div class="col-sm-8">
<input id="inputfeis" type="text" name="feisEntered" class="form-control" placeholder="Feis name - MM/DD/YYYY">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
And my query:
if(isset($_POST['submit'])){
$dancer=$_POST['dancer'];
$dancer = mysqli_real_escape_string($con,$dancer);
$level=$_POST['level'];
$level = mysqli_real_escape_string($con,$level);
$feisEntered=$_POST['feisEntered'];
$feisEntered = mysqli_real_escape_string($con,$feisEntered);
$danceName=$_POST['danceName'];
$danceName = mysqli_real_escape_string($con,$danceName);
$compNum=$_POST['compNum'];
$compNum = mysqli_real_escape_string($con,$compNum);
$competitor = $_POST['competitor'];
$competitor = mysqli_real_escape_string($con,$competitor);
$danceScore = $_POST['danceScore'];
$danceScore = mysqli_real_escape_string($con,$danceScore);
$placement = $_POST['placement'];
$placement = mysqli_real_escape_string($con,$placement);
$firstScore = $_POST['firstScore'];
$firstScore = mysqli_real_escape_string($con,$firstScore);
$secondScore = $_POST['secondScore'];
$secondScore = mysqli_real_escape_string($con,$secondScore);
$thirdScore = $_POST['thirdScore'];
$thirdScore = mysqli_real_escape_string($con,$thirdScore);
$judge = $_POST['judge'];
$judge = mysqli_real_escape_string($con,$judge);
$comments = $_POST['comments'];
$comments = mysqli_real_escape_string($con,$comments);
$query = "INSERT INTO reports (user_id, dancer_id1, dancer_name, competition_level1, feis_entered, dance_name1, competition_number1, number_competitors1, dancer_score1, dancer_placement1, firstpl_score1, 2ndpl_score1, 3rdpl_score1, judge_name1, judge_comment1) VALUES ('$id','$dancerID', '$dancer', '$level', '$feisEntered', '$danceName', '$compNum', '$competitor', '$danceScore', '$placement', '$firstScore', '$secondScore', '$thirdScore', '$judge', '$comments')";
$result = mysqli_query($con, $query);
}
So what I want to happen is a error message appears if they fill out both saying "Please choose only one". That way only one enters into database.
Never rely on just JS to validate input. Use JS to implement the behavior you speak of and then handle all possible outcomes in PHP. If someone is fiddling with your JS and bypasses the behavior you implement, you need to be able to handle that in your PHP code as well to make sure DB doesnt get unexpected input and break everything. Just like Justin said above, you could select one or the other, or throw an error. I recommend throwing an error and telling the user to make sure they have JS enabled.
In your HTML document, you should use Javascript to validate the user input before submitting the form to the PHP script. Then, in the PHP script, you should double-check that values were not received for both the select and the input (this could happen if the user does not have Javascript enabled, or they somehow bypass the validation) and determine what you want to do in that case (select one over the other, or exit with an error).
So I have made a form where staff members can post activities like soccer or tennis with a form. So I have 2 dates which they can select. The first date is the start date of the activity and the second date is the end date of the activity. I don't want it to be able to select an end date earlier than a selected startdate. So for example I want to fill in an activity like bowling and it in the form I put at the start date 03-04-2018 and the end date 02-02-2018, I don't want that to be possible.
This is how my form looks:
<?php echo form_open('index.php/Jongeren_activiteiten/Add_activiteit'); ?>
<br>
<center>Naam van activiteit:</center>
<div class="form-group">
<input class="form-control" name="activiteit" id="activiteit" type="text">
</div>
<center>Begindatum:</center>
<div class="form-group">
<input class="form-control" name="begindatum" id="begindatum" placeholder="Startdatum cursus" type="date">
</div>
<center>Einddatum:</center>
<div class="form-group">
<input class="form-control" name="einddatum" id="einddatum" placeholder="einddatum cursus" type="date">
</div>
<div>
<button class="btn btn-primary" name="Add_activiteit" >Toevoegen</button>
</div>
</form>
And this is the controller function of the form:
public function Add_activiteit()
{
if (isset($_POST['Add_activiteit'])) {
$this->form_validation->set_rules('activiteit', 'Activiteit', 'required');
$this->form_validation->set_rules('begindatum', 'Begindatum', 'required');
$this->form_validation->set_rules('einddatum', 'Einddatum', 'required');
//If form validation true
if ($this->form_validation->run() == TRUE) {
// echo 'form validated';
$data = array (
'activiteit'=>$_POST['activiteit'],
'begindatum'=>$_POST['begindatum'],
'einddatum'=>$_POST['einddatum'],
);
$this->db->insert('activiteit',$data);
$this->session->set_flashdata("success", "u heeft een nieuwe activiteit toegevoegd");
redirect("index.php/Jongeren_activiteiten", "refresh");
}
}
}
begindatum means start date and einddatum means end date. So how do I fix it so you can only select an end date later than a startdate?
I also don't want people to being able select a day in the past.
Any kind of help is appreciated
You should use a callback function..
The way you would do this is by creating a function like this:
function checkDate($start, $end){
if($start > $end){
$this->form_validation->set_message('checkDate', 'The end date must be later than the start date.');
return FALSE;
}else{
return TRUE;
}
}
Then you would add it to your form validation rules(I would do it for the end date), but prefix it with 'callback_'
So it would look like this:
$this->form_validation->set_rules('einddatum', 'Einddatum', 'required|callback_checkDate');
A further explanation provided by CodeIgniter can be found here: https://www.codeigniter.com/user_guide/libraries/form_validation.html
I am trying to import a csv file to my MySql database. In my page I will provide a sample csv file to download for users and in that csv file, users need to enter corresponding value and they will upload it. The values are getting inserted into the table. But, if I try to insert csv file into a table where we are having foreign keys then, I want the corresponding list in the csv for users to select its corresponding name from master table. Below is my database to which I need to import csv.
As you can see, parent_id is the id of subcategory table. I want the subcategory table's titles in dropdown list inside csv.
The expected output is like the given demo image below.
I am doing this in Codeigniter.
Controller Code
foreach ($csv_array as $row) {
$insert_data1 = array(
'parent_id'=>$row['Subcategory'],
'name' => $row['Name'],
'phone'=>$row['Phone'],
'mobile'=>$row['Mobile'],
'work_hr_from'=>$row['Work_hr_from'],
'work_hr_to'=>$row['Work_hr_to'],
'work_days'=>$row['Work_days'],
'address'=>$row['Address'],
'city_id'=>$row['City'],
'email'=>$row['Email'],
'website'=>$row['Website'],
'gmap_lat'=>$row['Gmap_latitude'],
'gmap_lng'=>$row['Gmap_longitude'],
'fax'=>$row['Fax'],
'start_year'=>$row['Start_year'],
);
$table ='item';
$st_id = $this->excel_data_insert_model->add_excel_details($insert_data1);
}
View Code
Code for downloading sample csv
Download Format
Form action to controller
<form action="<?php echo base_url();?>ExcelUpload/ExcelDataAdd" method="post" enctype="multipart/form-data">
<div class="box-body">
<div class="col-md-6">
<div class="form-group">
<label for="exampleInputFile">File input</label> <label style="color: red">*</label>
<input type="file" name="excel" id="excel" required="required">
</div>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
Model Code
public function add_excel_details($data_user){
$this->db->insert('item', $data_user);
}
Thanks in advance.
I've got 2 models (producten and voorraad), and one controller (producten).
So i've build a form inside the producten CRUD (create/update).
This is a custom form:
<div class="form-group">
<span class='btn btn-default addOption'>Optie toevoegen</span>
</div>
<div class='opties'>
</div>
<div class="form-group">
Verkrijgbaar als backorder?
<select name="backorder">
<option value="1">Ja</option>
<option value="0">Nee</option>
</select>
</div>
This is very easy. If you press on the button addOption, it will add a few form inputs.
Now when the form is being saved (here the trouble begins), it will always add the data from the form to the database. But i don't want that. I want the data to be checked first, if it already exsists, if so, it has to be replaced and not added again.
Is there a yii function for that, or how do I do this?
ProductenController piece:
foreach($_POST as $key => $value){
if(substr($key, 0,5) == 'maat_'){
$index_expl = explode('_',$key);
$index = $index_expl['1'];
$aantal = $_POST['aantal_'.$index];
$maten = explode(',',$value);
foreach($maten as $maat_key => $maat){
$kleuren = explode(',',$_POST['kleur_'.$index]);
foreach($kleuren as $kleur_key => $kleur){
$voorraad = new Voorraad();
$voorraad->product_id = $model->id;
$voorraad->maat = $maat;
$voorraad->kleur = $kleur;
$voorraad->aantal = $aantal;
$voorraad->backorder = (int)$_POST['backorder'];
$voorraad->save();
}
}
}
}
Replace the following :
$voorraad = new Voorraad();
with :
$voorraad = Voorraad::model()->findByPk($model->id); // assuming id is primary key, you can also use `findByAttributes`
if($voorraad == null) $voorraad = new Voorraad();
$voorraad->save(); is also used for updating the record.
just put a hidden input field in your form:
<div class="form-group hidden">
<input name="id" value="<?php echo YOURID;?>"/>
</div>
check if id exist in your server with empty(),if not just add;if exist then update.