in my where i use smarty to connect everything tpl file i have a form
<form method="post">
<div class="form-group"><label for="address"><strong>Address</strong></label><input class="border rounded-pill form-control" type="text" value='{$address}' name="address"></div>
<div class="form-row">
<div class="col">
<div class="form-group"><label for="city"><strong>City</strong></label><input class="border rounded-pill form-control" type="text" value='{$city}' name="city"></div>
</div>
<div class="col">
<div class="form-group"><label for="country"><strong>Country</strong></label><input class="border rounded-pill form-control" type="text" value='{$country}' placeholder="The Netherlands" name="country"></div>
</div>
</div>
<div class="form-group"><label for="phonenumber"><strong>Phone Number</strong></label><input class="border rounded-pill form-control" type="text" value='{$phone}' placeholder="+1 (0)1 234 56 789" name="phonenumber"></div>
</div>
<div class="card-body" style="background: #f5f5f5;">
<button class="btn btn-primary btn-sm" type="submit" name="contactsend">Save Settings</button>
</div>
</div>
</form>
But if i try to contact it in my php file
if(isset($_POST['contactsend'])) {
$inaddress = mysqli_real_escape_string($db, $_POST['address']);
$incity = mysqli_real_escape_string($db, $_POST['city']);
$insert = $db->query("UPDATE accounts SET address = ".$inaddress.", city = ". $incity ." WHERE id = ".$id_user."");
}
it wont get the contactsend button if i press it, so it also wont update the tables. can someone please help me?
that form is posting to it's own page ... is the php script in the same file as the render script? If not you need to set the action="" attribute of the form to the php script url
Related
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
I want to make a form using form repeater. As a result of my efforts somehow I could not register in the database.
html code:
<div class="m-portlet__body" id="myRadioGroup">
<div id="m_repeater_1">
<div class="form-group m-form__group row" id="m_repeater_1">
<div data-repeater-list="" class="col-md-12">
<div data-repeater-item class="form-group m-form__group row align-items-center">
<div class="col-md-12 m-form__group-sub">
<label class="form-control-label">Car plate</label>
<div class="input-group">
<input type="text" class="form-control" name="plate" placeholder="34 LAA 34" maxlength="10">
<div class="input-group-append">
<button data-repeater-delete="" class="btn btn-primary" type="button"><i class="la la-trash-o"></i></button>
</div>
</div>
</div>
</div>
</div>
<div class="m-form__group form-group row" style="padding-left: 48px; margin-top: -2rem;">
<div class="col-md-12">
<div data-repeater-create="" class="btn btn btn-sm btn-brand m-btn m-btn--icon m-btn--wide">
<span>
<i class="la la-plus"></i>
<span>add plate</span>
</span>
</div>
</div>
</div>
</div>
</div>
chrome looks like this in developer tools:
How should my database registration file be?
$plate = $_POST['plate'];
$sql = $db->prepare('INSERT INTO orders (plate) VALUES (?)');
$save = $sql->execute(array(
$plate,
));
The problem is in the html code, input name should be plate[1]. One possible cause is that you have id="m_repeater_1" declared twice. Make sure you are setting the template correctly first, and then you can search in all post data by using print_r($_POST); (with your current code no POST is being sent).
<input type="number" placeholder="Amount" name="repeat[0][amount]" class="form-control">
public function add()
{
$locations = $_POST['repeat'];
foreach ($locations as $key => $subValue) {
echo $subValue['amount'];
}
I'm trying to insert data to MySQL with a form that is in a Bootstrap modal but it doesn't work.
I don't know why but my form method is POST and it seems to be a GET because it prints the data in the web address.
When I try to insert the data through a basic form with the same php code (no format, simple as hell) it inserts the data.
Here's my form in the bootstrap modal.
<div class="container 2">
<button type="button" class="btn btn-primary btn-lg btn-block" data-toggle="modal" data-target="#newRoute">
CREAR NUEVA RUTA
</button>
<div class="modal fade" id="newRoute" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<form class="route" method="post">
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="form-group">
<label for="name">Ponle un nombre a la ruta</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="form-group">
<label for="city">Ciudad</label>
<input type="text" class="form-control" id="city" name="city" required>
</div>
<div class="form-group">
<label for="length">Distancia</label>
<input type="text" class="form-control" id="length" name="length" required>
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="form-group">
<label for="start_point">Punto de salida</label>
<input type="text" class="form-control" id="start_point" name="start_point"
required>
</div>
<div class="form-group">
<label for="difficulty">Dificultad</label>
<input type="text" class="form-control" id="difficulty" name="difficulty"
required>
</div>
<div class="form-group">
<label for="date">Fecha de la ruta</label>
<input type="date" class="form-control" id="date" name="date" required>
</div>
</div>
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="form-group">
<label for="description">Detalles de la ruta</label>
<textarea class="form-control" rows="5" id="description"
name="description"></textarea>
</div>
</div>
<div class="modal-footer">
<div class="form-group">
<button type="submit" name="submit" id="submit" class="btn btn-primary"
value="Enviar">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
and here is my PHP code
include('db/db.php');
if(isset($_POST['submit'])) {
// Adjudicar name a variable
$name = stripslashes($_POST['name']);
$name = mysqli_real_escape_string($conn, $name);
// Adjudicar city a variable
$city = stripslashes($_POST['city']);
$city = mysqli_real_escape_string($conn, $city);
// Adjudicar length a variable
$length = stripslashes($_POST['length']);
$length = mysqli_real_escape_string($conn, $length);
// Adjudicar start_point a variable
$start_point = stripslashes($_POST['start_point']);
$start_point = mysqli_real_escape_string($conn, $start_point);
// Adjudicar difficulty a variable
$difficulty = stripslashes($_POST['difficulty']);
$difficulty = mysqli_real_escape_string($conn, $difficulty);
// Adjudicar date a variable
$date = stripslashes($_POST['date']);
$date = mysqli_real_escape_string($conn, $date);
// Adjudicar description a variable
$description = stripslashes($_POST['description']);
$description = mysqli_real_escape_string($conn, $description);
// QUERY
$query = "INSERT INTO routes (name, city, length, start_point, difficulty, user_id, date, description) VALUES ('$name','$city',$length,'$start_point','$difficulty',".$_SESSION['id'].",'$date','$description')";
// Se realiza la query
$result = mysqli_query($conn,$query);
//CondiciĆ³n si se realiza la query correctamente
if($result){
header("Location: routes.php");
echo '<script type="text/javascript">alert("'.$query.'");</script>';
}else{
echo '<script>alert("ERROR");</script>';
}}
my db.php (connection to database)
$conn = new mysqli('localhost', 'root', "", 'users');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Thanks in advance!
PS. When I submit the form, this is what appears in the address (all the fields were filled by sample text). It seems to be a get form.
http://localhost/Proyecto/routes.php?name=Test&city=Test&length=20&start_point=Test&difficulty=Test&date=2018-05-12&description=Test+description&submit=Enviar
Try to check if you have forget to close any other form somewhere on your code that should be with method="get" or without method attribute
and so when you submit the data it gets the method from that form.
The code you have publish here should work correctly.
Your form code is correct, I have checked it on my system. You must be checking/testing some other page or form.
PHP File
<?php
session_start();
include 'dbconnect.php';
$emailuid = $_POST['emailuid'];
$pwd = $_POST['pwd1'];
$sql = "SELECT * FROM users WHERE uid='$emailuid' AND pwd='$pwd' OR
email='$emailuid' AND pwd='$pwd'";
$result = $result = $conn->query($sql);
if(!$row = $result->fetch_assoc()){
echo "Your Username or Password does not match";
}else{
$_SESSION['user_id'] = $row['user_id'];
header("Location: home.php");
}
?>
Html File
<div class="container">
<div class="jumbotron">
<div class="form-group">
<h1>Login</h1>
</div>
<form class="form-horizontal" action="login.php" method="post">
<div class="form-group input-group pb-modal-reglog-input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input class="form-control" name="emailuid" placeholder="Email or Username">
</div>
<div class="form-group input-group pb-modal-reglog-input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
<input type="password" class="form-control" name="pwd1" placeholder="password">
</div>
<div class="form-group">
<label>
<input type="checkbox">
Remember me.
</label>
</div>
<div class="form-group">
<button class="btn btn-primary col-lg-12" type="submit" name="btn_submit">Login</button>
</div>
<div class="form-group">
Forgot password?
</div>
</form>
</div>
</div>
</div>
`
Here's the code. Can someone please help me with this? Both codes are separate files, login.php and index.php now I want to display the error message inside my modal under the password input how will I do this? Thanks. I'm using bootstrap by the way and I'm still new to this together with php.
You can put the two files (php,html) submit the form to the same index.php then put condition statement to check if post request was made if it has then enter the checking block of your php code. Do the same checking in your html with inline php and if its post then check if it was sucessfull and display your message accordingly.
Without ajax i think this is the only solution i can think of
I have a form in HTML with some checkboxes and I want to send an email with the selected checkboxes in the body, the problem is that the "text" fields are passing to the PHP variables, but the checkboxes values are always set to uncheck when I test it on the PHP file:
HTML:
<div class="col-md-6">
<div class="c-checkbox-list">
<div class="c-checkbox">
<input type="checkbox" name="checkbox1citadina" id="checkbox1citadina" value="checkbox1citadina" class="form-control c-check" >
<label for="checkbox1citadina">
<span></span>
<span class="check"></span>
<span class="box"></span> Citadina
</label>
</div>
PHP:
$reservas_bicicletas = '';
if (isset($_POST['checkbox1citadina'])) {
$reservas_bicicletas .= "Citadina: checked\n";
} else {
$reservas_bicicletas .= "Citadina: unchecked\n";
}
echo $reservas_bicicletas;
$reservas_bicicletas always retrieves the else value.
Need help guys, thanks in advance.
remove c-check
<div class="col-md-6">
<div class="c-checkbox-list">
<div class="c-checkbox">
<input type="checkbox" name="checkbox1citadina" id="checkbox1citadina" value="checkbox1citadina" class="form-control " >
<label for="checkbox1citadina">
<span></span>
<span class="check"></span>
<span class="box"></span> Citadina</label>
</div>
Basically i have this (i shorted out the form because there are too many elements):
HTML:
<form name="quick_booking" id="quick_booking" method="post" action="assets/reserva-bicicletas.php">
<div class="col-md-6">
<div class="c-checkbox-list">
<div class="c-checkbox">
<input type="checkbox" name="checkbox1citadina" id="checkbox1citadina" value="checkbox1citadina" class="form-control " >
<label for="checkbox1citadina">
<span></span>
<span class="check"></span>
<span class="box"></span> Citadina</label>
</div>
<button type="submit" class="btn c-theme-btn c-btn-uppercase btn-lg c-btn-bold c-btn-square">Reservar</button>
</form>