I'm currently in an intership for a restaurant. I'm coding a website for them and I'm starting the CRUD on it.
The problem is that no data is passed to the controller, and so it doesn't update the drink.
Hope someone can help me.
I use a form to update a drink, here it is.
<div class="col-lg-6 order-lg-2">
<div class="p-5">
<p>Veuillez renseigner tous les champs !</p>
<p>Si un champ ne change pas, copier-coller ce champ depuis le tableau juste au-dessus.</p>
<form action="<?php base_url().'index.php/boissons/resp_modif_boisson/'.$son['id_boisson'] ?>" method="post">
<div class="form-group">
<label for="nom">Nom :</label>
<input type="text" class="form-control" id="nom" name="nom" maxlength="60" required>
</div>
<div class="form-group">
<label for="desc">Descriptif :</label>
<input type="text" class="form-control" id="desc" name="desc" maxlength="500" required>
</div>
<div class="form-group">
<label for="prix">Prix :</label>
<details>Si prix décimal, utilisez le "." ! (exemple : 1.99)</details>
<br>
<input type="number" class="form-control" id="prix" name="prix" min="0" step="0.01" value="0.00" required>
</div>
<button type="submit" class="btn btn-warning">Modifier</button>
</form>
</div>
</div>
And here is the controller.
public function resp_modif_boisson($num){
$this->load->helper('form');
$this->load->library('form_validation');
if($this->session->statut=="R"){
$this->form_validation->set_rules('nom', 'nom', 'required');
$this->form_validation->set_rules('desc', 'desc', 'required');
$this->form_validation->set_rules('prix', 'prix', 'required');
$nom = htmlspecialchars(addslashes($this->input->post('nom')));
$desc = htmlspecialchars(addslashes($this->input->post('desc')));
$prix = htmlspecialchars(addslashes($this->input->post('prix')));
var_dump($nom);
var_dump($desc);
var_dump($prix);
echo "test";
if ($this->form_validation->run() == FALSE){
$data['boi'] = $this->db_model->get_boisson($num);
echo "false";
$this->load->view('templates/haut_administrateur.php');
$this->load->view('resp_modif_boisson',$data);
$this->load->view('templates/bas');
}else{
$this->db_model->modif_boisson($num, $nom, $desc, $prix);
$data['boi'] = $this->db_model->get_boisson($num);
echo "true";
$this->load->view('templates/haut_administrateur.php');
$this->load->view('resp_modif_boisson',$data);
$this->load->view('templates/bas.php');
}
}else{
$data['boi'] = $this->db_model->get_all_boissons();
$this->load->view('templates/haut_accueil.php');
$this->load->view('boissons_afficher',$data);
$this->load->view('templates/bas.php');
}
}
Related
I am working on updating a profile information with a prefilled forms using the logged user information using CodeIgniter.
My problem is, whatever I put in the forms, the prebuild function form_validation->run() returns true, which means that the form fields are not correctly filled.
I don't think it has anything to do with my database.
Here is my view:
<form action="http://obiwan2.univ-brest.fr/licence/lic85/V1/CodeIgniter/index.php/vendeur/modifier_profil_vendeur" method="post">
<div class="form-group">
<label for="uname">Prénom:</label>
<?php echo'<input type="text" class="form-control" id="cmpt_prenom" placeholder="Entrez votre prenom" name="cmpt_prenom" value="'.$profil_vendeur->Prf_prenom.'">';?>
</div>
<div class="form-group">
<label for="uname">Nom:</label>
<?php echo'<input type="text" class="form-control" id="cmpt_nom" placeholder="Entrez votre nom" name="cmpt_nom" value="'.$profil_vendeur->Prf_nom.'">';?>
</div>
<div class="form-group">
<label for="uname">adresse e-mail:</label>
<?php echo'<input type="email" class="form-control" id="cmpt_email" placeholder="Entrez votre e-mail" name="cmpt_email" value="'.$profil_vendeur->Prf_mail.'">';?>
</div>
<div class="form-group">
<label for="pwd">Mot de passe:</label>
<input type="password" class="form-control" id="mdp" placeholder="Entrez votre mot de passe" name="mdp">
</div>
<div class="form-group">
<label for="pwd">Confirmation du mot de passe:</label>
<input type="password" class="form-control" id="conf_mdp" placeholder="Confirmez votre mot de passe" name="conf_mdp">
</div>
<button type="submit" class="btn btn-success">Valider</button>
</form>
Here is my controller function i'm using :
if ($this->form_validation->run() == FALSE) {
if ($mdp1==$mdp2) {
$this->db_model->update_prf_nom_prenom_mail_vendeur($prenom,$nom,$mail,$mdp1);
} else {
$mdp['mdp']=$this->db_model->get_prf_nom_prenom_mail();
$this->db_model->update_prf_nom_prenom_mail_vendeur($prenom,$nom,$mail,$mdp['mdp']->Cmpt_mdp);
}
} else {
if ($this->session->statut=="V") {
$data['profil_vendeur']=$this->db_model->get_prf_nom_prenom_mail();
$this->load->view('templates/vendeur_haut');
echo("Veuillez remplir au moins les champs : Prénom, nom, mail");
$this->load->view('vendeur_modifications_compte.php',$data);
$this->load->view('templates/bas');
}
}
Thanks for the time you put in helping me to resolving this issue.
Try this. Please add $this->form_validation->set_rules in the controller part.
$this->form_validation->set_rules('cmpt_prenom', 'cmpt_prenom', 'required');
$this->form_validation->set_rules('cmpt_nom', 'cmpt_nom', 'required');
$this->form_validation->set_rules('cmpt_email', 'cmpt_email', 'required');
$this->form_validation->set_rules('mdp', 'mdp', 'required');
$this->form_validation->set_rules('conf_mdp', 'conf_mdp', 'required');
if ($this->form_validation->run() == FALSE) {
if ($mdp1==$mdp2) {
$this->db_model->update_prf_nom_prenom_mail_vendeur($prenom,$nom,$mail,$mdp1);
} else {
$mdp['mdp']=$this->db_model->get_prf_nom_prenom_mail();
$this->db_model->update_prf_nom_prenom_mail_vendeur($prenom,$nom,$mail,$mdp['mdp']->Cmpt_mdp);
}
} else {
if ($this->session->statut=="V") {
$data['profil_vendeur']=$this->db_model->get_prf_nom_prenom_mail();
$this->load->view('templates/vendeur_haut');
echo("Veuillez remplir au moins les champs : Prénom, nom, mail");
$this->load->view('vendeur_modifications_compte.php',$data);
$this->load->view('templates/bas');
}
}
I have a website where the user can create a profile. However, if he decides to make any alteration on that profile, he can go to alterar_cadastro.php and make that changes he desire. Code of the file bellow:
<?php
session_start();
include('verifica_login.php');
$email = $_SESSION['usuario'];
$busca_nivel = "SELECT * FROM cadastro WHERE email = '$email' OR sobrenome = '$email'";
$resultado_busca_nivel = mysqli_query($conexao, $busca_nivel);
$rows = mysqli_fetch_array($resultado_busca_nivel);
?>
<html>
<head>
</head>
<body>
<div class="row">
<div class="col-1"></div>
<div class="col-10">
<br>
<h3>Preencha os dados abaixo para alterar seu cadastro:</h3>
<br>
<form method="post" action="processa_alteracao.php" enctype="multipart/form-data">
<div class="form-group">
<label for="nome">Nome: </label>
<input class="form-control" type="text" name="nome" placeholder="<?php echo $rows['nome'] ?>"><br>
</div>
<div class="form-group">
<label class="form-group">Apelido: </label>
<input class="form-control" type="text" name="sobrenome" placeholder="<?php echo $rows['sobrenome'] ?>">
<small class="form-text text-muted">Atenção! Mudar seu apelido significa mudar o seu login.</small>
</div>
<div class="form-group">
<label for="nome">Cidade: </label>
<input class="form-control" type="text" name="cidade" placeholder="<?php echo $rows['cidade'] ?>"><br>
</div>
<div class="form-group">
<label class="form-group">Estado: </label>
<input class="form-control" type="text" name="estado" placeholder="<?php echo $rows['estado'] ?>">
<small class="form-text text-muted">Escreva o seu estado de forma abreviada. Ex.: AM, AC, DF, TO.</small>
</div>
<div class="form-group">
<label class="form-group">Quem é você? </label>
<textarea class="form-control" rows="5" name="geral" placeholder="<?php echo $rows['geral'] ?>"></textarea>
<small class="form-text text-muted">Nos fale um pouquinho sobre quem é você e o que espera da nossa plataforma :) Máximo de caracteres: 100.</small>
</div>
<div class="form-group">
<label class="form-group">E-mail: </label>
<input class="form-control" type="text" name="email" placeholder="<?php echo $rows['email']; ?>" readonly>
</div>
<div class="form-group">
<label class="form-group">Foto para o perfil: </label><input type="file" name="foto_perfil">
</div>
<div class="form-group">
<label class="form-group">Senha: </label><input class="form-control" type="password" name="senha" placeholder="Insira sua senha para confirmar"><br><br>
<button class="btn btn-primary" type="submit">Alterar Perfil</button>
</div>
</form>
</div>
<div class="col-1"></div>
</div>
</body>
</html>
However, if the user leaves one of the inputs blank, that's the value going to MYSQL. I put bellow the php file that process this information:
<?php
session_start();
include 'db.php';
$email = $_SESSION['usuario'];
$nome = mysqli_real_escape_string($conexao, $_POST['nome']);
$sobrenome = mysqli_real_escape_string($conexao, $_POST['sobrenome']);
$cidade = mysqli_real_escape_string($conexao, $_POST['cidade']);
$estado = mysqli_real_escape_string($conexao, $_POST['estado']);
$geral = mysqli_real_escape_string($conexao, $_POST['geral']);
$perfil = $_FILES['foto_perfil'];
$senha = mysqli_real_escape_string($conexao, $_POST['senha']);
$query_cad = "SELECT * FROM cadastro WHERE email = '$email' AND senha = '$senha' OR sobrenome = '$sobrenome' AND senha = '$senha'";
$result = mysqli_query($conexao, $query_cad);
$row = mysqli_num_rows($result);
$row2 = mysqli_fetch_array($result);
$fk_cad = $row2['entry_cod_cad'];
if($row == 1) {
if(isset($perfil)){
$extensao = strtolower(substr($_FILES['foto_perfil']['name'], -4));
$novo_nome = md5(time()) . $extensao;
$diretorio = "img/perfil/";
move_uploaded_file($_FILES['foto_perfil']['tmp_name'], $diretorio.$novo_nome);
}
$query = "UPDATE cadastro SET nome = '$nome', sobrenome = '$sobrenome', cidade = '$cidade', estado = '$estado', geral = '$geral', perfil = '$novo_nome' WHERE entry_cod_cad = '$fk_cad'";
$result_alteracao = mysqli_query($conexao, $query);
$msg = "success";
echo "<script>alert('$msg');window.location.assign('/index.php?pagina=perfil');</script>";
} else {
$msg = "incorrect";
echo "<script>alert('$msg');window.location.assign('/index.php?pagina=inicio');</script>";
}
My question is: How do I make the empty fields have a pre existing value, where this value is the one that already is in his profile. I thought that the value attribute inside <input> would do the trick, however, I was not successful.
Use value parameter instead of placeholder
<?php
session_start();
include('verifica_login.php');
$email = $_SESSION['usuario'];
$busca_nivel = "SELECT * FROM cadastro WHERE email = '$email' OR sobrenome = '$email'";
$resultado_busca_nivel = mysqli_query($conexao, $busca_nivel);
$rows = mysqli_fetch_array($resultado_busca_nivel);
?>
<html>
<head>
</head>
<body>
<div class="row">
<div class="col-1"></div>
<div class="col-10">
<br>
<h3>Preencha os dados abaixo para alterar seu cadastro:</h3>
<br>
<form method="post" action="processa_alteracao.php" enctype="multipart/form-data">
<div class="form-group">
<label for="nome">Nome: </label>
<input class="form-control" type="text" name="nome" value="<?php echo $rows['nome'] ?>"><br>
</div>
<div class="form-group">
<label class="form-group">Apelido: </label>
<input class="form-control" type="text" name="sobrenome" value="<?php echo $rows['sobrenome'] ?>">
<small class="form-text text-muted">Atenção! Mudar seu apelido significa mudar o seu login.</small>
</div>
<div class="form-group">
<label for="nome">Cidade: </label>
<input class="form-control" type="text" name="cidade" value="<?php echo $rows['cidade'] ?>"><br>
</div>
<div class="form-group">
<label class="form-group">Estado: </label>
<input class="form-control" type="text" name="estado" value="<?php echo $rows['estado'] ?>">
<small class="form-text text-muted">Escreva o seu estado de forma abreviada. Ex.: AM, AC, DF, TO.</small>
</div>
<div class="form-group">
<label class="form-group">Quem é você? </label>
<textarea class="form-control" rows="5" name="geral" value="<?php echo $rows['geral'] ?>"></textarea>
<small class="form-text text-muted">Nos fale um pouquinho sobre quem é você e o que espera da nossa plataforma :) Máximo de caracteres: 100.</small>
</div>
<div class="form-group">
<label class="form-group">E-mail: </label>
<input class="form-control" type="text" name="email" value="<?php echo $rows['email']; ?>" readonly>
</div>
<div class="form-group">
<label class="form-group">Foto para o perfil: </label><input type="file" name="foto_perfil">
</div>
<div class="form-group">
<label class="form-group">Senha: </label><input class="form-control" type="password" name="senha" placeholder="Insira sua senha para confirmar"><br><br>
<button class="btn btn-primary" type="submit">Alterar Perfil</button>
</div>
</form>
</div>
<div class="col-1"></div>
</div>
</body>
</html>
Hi I'm making a form with a textarea using TinyMCE and when i press the submit button to send it to php DB, the button does nothing. I've put the textarea in commentary to see if it was the problem and it work so i've deduced that it was the problem.
There is my form code :
<div class="col-sm-12">
<form id="RedactionForm" action="redaction_post.php" method="post">
<?php
// Connexion à la base de données
try
{
$bdd = new PDO('mysql:host=localhost;dbname=utilisateur;charset=utf8', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
date_default_timezone_set('Europe/Paris');
$date_creation = gmdate('Y-m-d h:i:s');
?>
<div class="col-sm-6 form-group">
<label for="titre" class="control-label" id="label-redac">Titre</label>
<input type="text" class="form-control" name ="titre" id="titre" placeholder="Le titre" data-title="Le titre est obligatoire" required="" data-regex="^[a-zA-Z]{1,150}$">
</div>
<div class="col-sm-6 form-group">
<label for="description" class="control-label" id="label-redac">Descritpion</label>
<input type="text" class="form-control" name ="description" id="description" placeholder="Une brève description de l'article" data-title="La description est obligatoire" required="" data-regex="^[a-zA-Z]{1,255}$">
</div>
<div class="col-sm-12 form-group">
<label for="contenu" class="control-label" id="label-redac2">Article</label>
<textarea class="form-control" name="contenu" id="contenu" placeholder="Rédigez l'article ici" data-title="Le contenu de l'article est obligatoire" required=""></textarea>
</div>
<input type='hidden' name='date_creation' id="date_creation" value="<?php echo "$date_creation";?>" />
<div class="alert alert-danger">
Error!
</div>
<div class="alert alert-sucess">
Sent!
</div>
<div class="form-group">
<input type="submit" value="Envoyer l'article" class="btn-primary btn-sm"/>
<!--<button type="submit" class="btn-primary btn-sm">Envoyer l'article</button>-->
<a class="btn-primary btn-sm" href="./index.php"><span class="glyphicon glyphicon-chevron-left"></span> Retour à la liste des articles</a>
</div>
</form>
</div>
dont use "required" in tag <textarea>
change sintaks
<textarea class="form-control" name="contenu" id="contenu" placeholder="Rédigez l'article ici" data-title="Le contenu de l'article est obligatoire" required=""></textarea>
to
<textarea class="form-control" name="contenu" id="contenu" placeholder="Rédigez l'article ici" data-title="Le contenu de l'article est obligatoire"></textarea>
maybe help you..
i have a problem with the validation form. It does not work if i put "required", example:
controller:
public function updateBenefit(){
$result = array();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('beneficio', 'Nombre del Beneficio', 'required|alpha');
$this->form_validation->set_rules('info', 'Info', 'alpha');
$this->form_validation->set_rules('descrip', 'Descripción', 'alpha');
$this->form_validation->set_rules('orden', 'Orden', 'integer');
// $this->form_validation->set_rules('fecha', 'Fecha', 'date_valid');
$this->form_validation->set_message('required', 'El campo %s es requerido');
if ($this->form_validation->run() == TRUE){
if (isset($_POST['id'])){
$idb = $_POST['id'];
$benefit = BeneficiosManager::getInstance()->getHome($idb);
$result['message'] = "Se ha modificado el Beneficio con éxito";
} else{
$benefit = BeneficiosManager::getInstance()->create();
$result['message'] = "Se ha cargado el Beneficio con éxito";
}
$benefit->nombre = ucfirst(strtolower($_POST['beneficio']));
$benefit->content = ucfirst(strtolower($_POST['descrip']));
$benefit->intro = ucfirst(strtolower($_POST['info']));
$benefit->active = $_POST['optionsRadios2'];
$benefit->orden = $_POST['orden'];
// $benefit->date = $_POST['fecha'];
BeneficiosManager::getInstance()->save($benefit);
}else{
//no se validaron los datos ingresados
$result['message'] = "Error validación";
}
echo json_encode($result);
}
view:
{extends file='admin/base/base.tpl'}
{block name='content'}
<h3>Cargar Beneficio </h3>
</br>
<form action="{site_url()}admin/updateBenefit" class="form-horizontal" method="post" id="" enctype="multipart/form-data">
<div class="control-group">
<label class="control-label">Beneficio</label>
<div class="controls">
<input type="text" name="beneficio" value="" class="m-wrap medium" />
<span class="help-inline">Nombre del Beneficio</span>
</div>
</div>
<div class="control-group">
<label class="control-label">Info</label>
<div class="controls">
<textarea name="info" class="medium m-wrap" rows="3"></textarea>
<span class="help-inline">Información Clave</span>
</div>
</div>
<div class="control-group">
<label class="control-label">Descripción</label>
<div class="controls">
<textarea name="descrip" class="large m-wrap" rows="3"></textarea>
<span class="help-inline">Descripción del Beneficio</span>
</div>
</div>
<div class="control-group">
<label class="control-label">Activo</label>
<div class="controls">
<label class="radio line">
<input type="radio" name="optionsRadios2" value="1"/>Si</input>
</label>
<label class="radio line">
<input type="radio" name="optionsRadios2" value="0"/>No</input>
</label>
<span class="help-inline">Ofrecer Beneficio</span>
</div>
</div>
<div class="control-group">
<label class="control-label">Orden</label>
<div class="controls">
<input type="text" name="orden" value="" class="m-wrap small" />
<span class="help-inline">Prioridad del Beneficio</span>
</div>
</div>
<div class="control-group">
<label class="control-label">Fecha</label>
<div class="controls">
<input type="text" name="fecha" value="{$smarty.now|date_format}" class="m-wrap medium" />
<span class="help-inline"></span>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn blue"><i class="icon-ok"></i> Guardar</button>
<button type="button" class="btn">Cancelar</button>
</div>
</form>
{/block}
what might the problem be?
if i remove the "required" field, the form validates... but if i put it, it doesn't... i don't know what else to try, can't understand why this is happening
the code is working now, thanks to #Jonathan that corrected me, and i was also making an imput mistake when i was trying this method. I was trying to put two words in the same field (i.e: bon vivir); so the correct input would be: bonvivir.
I'm not sure you are using "title" as the name of your real title input element. Because I found you use this code to assign your title.
$benefit->title = ucfirst(strtolower(trim($_POST['beneficio'])));
So you may want to try to use
$this->form_validation->set_rules('beneficio', 'Nombre del Beneficio', 'required|alpha');
instead.
I am not sure I had the right guess. Just give it a go. Hope this helps.
In my controller, i need some instructions to be launched only if a form has been sent.
This is my controller:
public function indexAction()
{
$form = array();
$submit = $this->getRequest()->getParam('submit');
if (!empty($submit))
{
$lastname = $this->getRequest()->getParam('last-name');
$name = $this->getRequest()->getParam('name');
$email = $this->getRequest()->getParam('email');
$form = array(
'lastname' => $lastname,
'name' => $name,
'email' => $email);
$confirm = Tools::checkInscription($form);
var_dump($confirm);
exit();
if ($confirm === true)
{
Tools::saveUser($form);
}
else
{
// Mets une variable a true pour savoir dans ta vu que tu as une erreur.
$this->_redirect('/inscription');
}
}
}
the problem is $submit always seems returning null.
My view:
<form id="formulaire">
<div class="msg">
<p class="error">Merci de vérifier les champs en rouge</p>
</div>
<div class="last-name">
<input type="text" class="error" id="last-name" name="last-name" placeholder="Votre nom" />
</div>
<div class="name">
<input type="text" id="name" name="name" placeholder="Votre prénom"/>
</div>
<div class="email">
<input type="text" id="email" name="email" placeholder="Votre email"/>
</div>
<button type="submit" title="Valider"></button>
</form>
Can anyone help to find what i'm doing wrong
thanks in advance
If you set your form method to POST you can check it with this:
if ($this->getRequest()->isPost()) {
}
Your submit button should be an input :
<input type="submit" title="Valider" />
And your form should have an action :
<form id="formulaire" method="POST" action="some_page.php">
Try
<input type="submit" title="Valider">
instead of
<button type="submit" title="Valider"></button>
And change
<form id="formulaire" action="" method="post">
instead of
<form id="formulaire">