how to show warning of empty field on form validation? - php

these are my code for form validation :
<form action="<?php echo base_url().'data_laporan/tambah_aksi';?>"method="post" enctype="multipart/form-data">
<div class="form-group">
<label>Jenis Laporan</label>
<select class="form-control" name="jenis_laporan">
<option>Penemuan hewan</option>
<option>Kehilangan Hewan</option>
</select>
</div>
<div class="form-group">
<label>Jenis Hewan</label>
<input type="text" name="jenis_hewan" class="form-control">
</div>
<div class="form-group">
<label>Lokasi</label>
<input type="text" name="lokasi" class="form-control">
</div>
<div class="form-group">
<label>Deskripsi</label>
<input type="text" name="deskripsi" class="form-control">
</div>
<div class="form-group">
<label>Nama Pelapor</label>
<input type="text" name="nama_pelapor" class="form-control">
</div>
<div class="form-group">
<label>No Hp</label>
<input type="text" name="no_hp" class="form-control">
</div>
<div class="form-group">
<label>Gambar Hewan</label>
<input type="file" name="gambar" class="form-control">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
i want to show a simple warning when i click "Save Changes" as a submit button like this :
image for example warning
and these are my code for tambah_aksi() function :
public function tambah_aksi()
{
$jenis_laporan = $this->input->post('jenis_laporan');
$jenis_hewan = $this->input->post('jenis_hewan');
$deskripsi = $this->input->post('deskripsi');
$lokasi = $this->input->post('lokasi');
$nama_pelapor = $this->input->post('nama_pelapor');
$no_hp = $this->input->post('no_hp');
$gambar = $_FILES['gambar']['name'];
if ($gambar = '') {} else{
$config ['upload_path'] = './upload';
$config ['allowed_types'] = 'jpg|jpeg|png|gif';
$this->load->library('upload', $config);
if(!$this->upload->do_upload('gambar')){
echo "Gambar gagal di Upload";
}else{
$gambar=$this->upload->data('file_name');
}
}
$data = array (
'jenis_laporan' => $jenis_laporan,
'jenis_hewan' => $jenis_hewan,
'deskripsi' => $deskripsi,
'lokasi' => $lokasi,
'nama_pelapor' => $nama_pelapor,
'no_hp' => $no_hp,
'gambar' => $gambar,
);
$this->model_laporan->tambah_aksi($data, 'tb_laporan');
redirect('data_laporan/index');
}
i tried using if-else form validation run checking but it does not goes well, could you please help me to fix this with another way or the efficient way of if-else checking?

You can do this many ways
Via codeigniter 3.x
then first load form validation library in controller function and set rules and place holder for showing errors in view file. See for reference
https://codeigniter.com/userguide3/libraries/form_validation.html#the-controller
https://codeigniter.com/userguide3/libraries/form_validation.html#showing-errors-individually
Via HTML
You can simply do this by adding required attribute to all html elements which are required, browser will show warning automatically after clicking on submit button for required fields. see example for input html element
<input type="text" name="nama_pelapor" required class="form-control">
See for dropdown
<select class="form-control" required name="jenis_laporan">
<option>Penemuan hewan</option>
<option>Kehilangan Hewan</option>
</select>
Via Jquery
If you want to customize error messages then you do this via your own code in jquery or jquery validator library. See this documentation for jquery validation
https://jqueryvalidation.org/validate/

Comparison operator should be double == not single =. Edit your line of code to this.
if ($gambar =='') {} else{

Related

How to pass value of a form to another form PHP?

I have a form A in index.php which I need to pass the value of 'notebook_id' to formB in create_new_note.php. I used POST method to do this but it is not working.
form A in index.php
<!-- Modal -->
<div class="modal" id="newNotebookModal" tabindex="-1" aria-labelledby="newNotebookModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<center><h5 class="modal-title" id="newNotebookModalLabel">Create a New Notebook</h5></center>
</div>
<div class="modal-body">
<form class="forms-sample" method="post">
<div class="form-group">
<label for="notebook_name_label">Notebook Name</label>
<input type="text" class="form-control" name="notebook_name" id="notebook_name" placeholder="Notebook Name">
</div>
</div>
<div class="modal-footer">
<button class="btn btn-outline-secondary btn-fw">Cancel</button>
<input type="submit" class="btn btn-primary" name="submit" value="Create"/>
</div>
</form>
</div>
</div>
</div>
</div>
Form B
<form class="forms-sample" method="post">
<div class="form-group">
<label for="note_title_label">Title</label>
<input type="hidden" name="notebook_id" value="<?php echo $notebook_id; ?>">
<input type="text" class="form-control" name="note_title" id="note_title" placeholder="Title">
</div>
<div class="form-group">
<textarea class="form-control" name ="note_content"id="note_content" rows="40"></textarea>
</div>
<button class="btn btn-outline-secondary btn-fw">Cancel</button>
<input type="submit" class="btn btn-primary" name="submit" value="Create"/>
</form>
I used MVC framework, hence this is my Controller code and Model code
// Controller
function addNotebook($std_id) {
$notebook = new ManageNotesModel();
$notebook->std_id = $std_id;
$notebook->notebook_name = $_POST['notebook_name'];
if($notebook->addNotebook() > 0) {
$message = "Your notebook has been created!";
echo "<script type='text/javascript'>alert('$message');
window.location = '../ManageNotesView/create_new_note.php'</script>";
}
}
// Model
function addNotebook(){
$sql = "insert into notebook(std_id, notebook_name)values(:std_id, :notebook_name)";
$args = [':std_id'=> $this->std_id, ':notebook_name'=>$this->notebook_name];
$stmt = DB::run($sql, $args);
$count = $stmt->rowCount();
return $count;
}
Please help me, I'm new to this I tried to make way through it, but cant seem to figure out whats wrong
As I see your code is creating a new row in the database. After saving row into database you want to know the last inserted id of the entity called Notebooks. Reuse that value and pass it into your template of creating titles. If your MVC framework does not know the last inserted id, you should customize it and add functionality to do that.
Check last_inserted_id

Submit form with php WordPress

Hello I work with WordPress , and I create a custom Modal with form to add a Group called 'abonnement' I added the table in Database and this is my issue:
Form
<form name="abonnementCreate" action="create.php" method="POST">
<div class="form-group">
<label>Nom de l'abonnemnt</label>
<input type="text" class="form-control" id="nomA" name="nomA">
</div>
<div class="form-group">
<label>Durée</label>
<input type="number" class="form-control" id="dureeA">
<small id="dureeHelp" class="form-text text-muted">En jours</small>
</div>
<div class="form-group">
<label>Nombre de produit autorisé</label>
<input type="number" class="form-control" id="nbProdA">
</div>
<div class="form-group">
<label>Frais d'abonnement (€)</label>
<input type="number" class="form-control" id="fraisA">
</div>
<button type="submit" name="create" class="btn btn-primary">Valider</button>
</form>
and this is the action of the creation :
create.php
<?php
include'admin.php';
global $wpdb;
if(isset( $_POST["create"]))
{
$wpdb->insert('abonnement', array(
'nom' => '.$_POST["nomA"].',
'duree' => '.$_POST["dureeA"].',
'nbProd' => '.$_POST["nbProdA"].',
'frais' => '.$_POST["fraisA"].'
));
}
?>
I have $_POST["create"] and the condition is true but $_POST[" any field"] is always null,
Someone please can help me ?
you can never catch the post value from id of the form input field.only way that you can get value using $_POST[] is using the "name" attribute of the input field.
<input type="text" class="form-control" id="nomA" name="name1">
this input field value can be accessed from form action php file using $_POST["name1"]

form validation using ajax in codeigniter

In this when i submit my form it shows errors first then reload the page and go to index page.. but i want is show only error message not reload page after.. here my controller is
class User extends CI_Controller {
public function index()
{
$this->load->view('index');
}
public function error()
{
/* Load form helper */
$this->load->helper(array('form'));
/* Load form validation library */
$this->load->library('form_validation');
/* Set validation rule for name field in the form */
$form_data = array('brand_name' => $this->input->post('brand_name'), 'your_name ' => $this->input->post('your_name'), 'mobile_no' => $this->input->post('Mobile No.'));
if($this->form_validation->run($form_data) == FALSE )
{
echo validation_errors();
}
else
{
$this->load->view('submit');
}
}
}
and my view file named index.php is
<script type="text/javascript">
$(document).ready(function(){
$('#get').click(function(){
$.post("<? base_url() . 'user/error' ?>",
$("form").serialize(),
function(result) {
$("#error_message").html(result).appendTo("form");
},"html");
});
});
</script>
<form role="form" action="" class="form-inline" name="form" method="POST" id >
<div id="error_message" style="color:red"></div>
<?php echo form_open('form'); ?>
<div class="col-md-3">
</div>
<div class="col-md-2">
<div class="form-group">
<input type="text" class="form-control" placeholder="Brand Name" name="brand_name" onfocus="this.placeholder = ''" onblur="this.placeholder='Brand Name'">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Name" name="your_name" onfocus="this.placeholder = ''" onblur="this.placeholder='Your Name'">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input type="text" class="form-control" placeholder="Mobile No" name="mobile_no" onfocus="this.placeholder = ''" onblur="this.placeholder='Mobile No'">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input type="submit" id="get" class="btn btn-success" name="submit" value="Proceed" ></input>
</div>
</div>
<div class="col-md-3">
</div>
</form>
i just want it shows error message only not reload page after please help... thanks in advance
You are trying to post form in AJAX so you will need to set the form data manually
Tested with CI 2
$form_data = array('brand_name' => $this->input->post('brand_name'), 'your_name ' => $this->input->post('your_name')); // other fields as required for validations
And validation for the same would be
if($this->form_validation->run($form_data) == TRUE ){
//validation passed
}else{
//validation fail
}
For CodeIgniter 3, as the Online documentation mentions try https://www.codeigniter.com/userguide3/libraries/form_validation.html#validating-an-array-other-than-post
Validating an Array (other than $_POST)
Sometimes you may want to validate an array that does not originate from $_POST data.
In this case, you can specify the array to be validated:
$data = array(
'username' => 'johndoe',
'password' => 'mypassword',
'passconf' => 'mypassword'
);
$this->form_validation->set_data($data);

Get Value From Textbox to File_get_contents

<div class="form-group">
<label for="inputsm">Put URL</label>
<input class="form-control input-sm" id="inputsm" type="text">
<br>
<input type="submit" class="btn btn-info" value="Count Contact">
</div>
This code is my text box and button
<?php
$getText = file_get_contents("", true);
$Contact = substr_count($getText ,"CONTACTID");
print_r($Contact);
?>
and this code is php for get a value and count
Example
I need to put this link https://s3-ap-southeast-1.amazonaws.com/cloudpoc2/us-east-1%3A4502ecdd-1994-40da-8eb2-b6ccc96d6be0/Contacts/Contact_2014_11_19_09_53_28_278.vcf in text box
and click button it show number of contact in this file
$getText = file_get_contents("https://s3-ap-southeast-1.amazonaws.com/cloudpoc2/us-east-1%3A4502ecdd-1994-40da-8eb2-b6ccc96d6be0/Contacts/Contact_2014_11_19_09_53_28_278.vcf", true);
You have to submit your form through POST or GET. My example will use POST. You also must assign a name to the elements you're posting (such as URL). This is untested, so it may have some errors. But it will give you an idea.
<?php
if(isset($_POST['submit'])){
$url = $_POST['url'];
$getText = file_get_contents("$url", true);
$Contact = substr_count($getText ,"CONTACTID");
print_r($Contact);
}
?>
<form method="POST" action="?">
<div class="form-group">
<label for="inputsm">Put URL</label>
<input name="url" class="form-control input-sm" id="inputsm" type="text">
<br>
<input name="submit" type="submit" class="btn btn-info" value="Count Contact">
</div>
</form>

Form isn't picking up the variables to post

My test PHP submission code:
<?php
if($_POST['create']) { echo $_POST['name']; }
?>
My HTML form code:
<form class="form-horizontal" id="ConsultantSignUp" method="post" action="#">
<div class="control-group">
<label class="control-label" for="inputForename">Name</label>
<div class="controls">
<input type="text" class="input-xlarge" id="name" name="name" rel="popover" data-content="Enter your first and last name." data-original-title="Full Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="input01"></label>
<div class="controls">
<button type="submit" class="btn btn-success" rel="create" title="create" id="create" name="create">Create My Account</button>
</div>
</div>
</form>
My error is that it doesn't even print out the name after submission. I've checked the PHP Error log and it says:
PHP Notice: Undefined index: create in /Users/**/sites/signup.php on line 2
Line 2 being 'if($_POST['create']) { echo $_POST['name']; }'.
I'm aware of the isset() method to remove the Undefined Index notice, how ever, I just want to test my form!
This is my total code...This working perfectly for me in Chrome, Explorer and Firefox. Just check it
<?php
if(isset($_POST['create']))
{
echo $_POST['name'];
}
else
{
?>
<form class="form-horizontal" id="ConsultantSignUp" method="post" action="#">
<div class="control-group">
<label class="control-label" for="inputForename">Name</label>
<div class="controls">
<input type="text" class="input-xlarge" id="name" name="name" rel="popover" data-content="Enter your first and last name." data-original-title="Full Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="input01"></label>
<div class="controls">
<button type="submit" class="btn btn-success" rel="create" title="create" id="create" name="create">Create My Account</button>
</div>
</div>
<?php
}
?>
Your <button ... name="create"> doesn't have a value so it will submit an empty string and $_POST['create'] will evaluate as false.
Give it a value.
the 'undefined index' NOTICE will go away when you use isset() function.
this often occurs when an unchecked checkbox is also posted.
the error is only there because no 'value' is passed on the submit button and the the submit button is POSTED empty.
replace submit button with,
<button value='submitbtn' type="submit" class="btn btn-success" rel="create" title="create" id="create" name="create">Create My Account</button>
PS ONLY (value='submitbtn') is added to your original button

Categories