Cannot handle a form in php - php

I'm learning now php and i'm stuck in one place with handling form submit action.
Im my input i'm trying to store user name in $_GET['firstname'] variable. But it's empty. I mean, that after checking if $_GET['firstname'] isset I get false. Where is my mistake?
<body>
<div class="container">
<div class="section back">
<form class="form myform" action="addcustomer.php" method="get">
<span class="myformtitle">
Add new User
</span>
<div class="form-group">
<div class="col validate-input">
<span class="label-input">Firstname</span>
<input id="firstname" class="input myinput" type="text" name="firstname" placeholder="Enter your firstname" value="<?php if (isset($_GET['firstname'])) echo $_GET['firstname']?>">
<span class="focus-input"></span>
</div>
</div>
<div class="col col-btn">
<button type="button" name="submit" class="btn sb-btn btn-block">
<span class="btn-sp">
Submit
</span>
</button>
</div>
</form>
</div>
</div>
<?php
if (isset($_GET['firstname'])) {
echo '<script>console.log("' . $_GET['firstname'] . '")</script>';
} else {
echo '<script>console.log("no name")</script>';
}
?>
</body>

Change your button type from button to submit.
button types are for Javascript (JS).
submit types are used to process PHP (form) directives.

Related

HTML form not passing checkbox value trough $_POST in php file

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>

Submit button doesn't fire

My submit button suddenly stopped working. It submits data into a MySQL database.
While I was doing some design changes, it suddenly stopped working.
Are there any apparent errors/mistakes in the code below?
I'm a noob who's currently trying to learn some PHP and so on, so any help would be much appreciated. :)
<section id="moviesearch">
<!-- Section -->
<div class="subcribe2">
<div class="container">
<!-- Container -->
<div class="row">
<!-- Row -->
<div class="col-md-4">
</div>
<body ng-app="myApp">
<div ng-controller="MyCtrl" class="col-md-4">
<form class="form-watch-list-create">
<input required="required" placeholder="Movie title" type="text" class="form-control" typeahead="item.Title for item in getLocation($viewValue)" typeahead-loading="loadingLocations" typeahead-min-length="2" typeahead-wait-ms="1000" typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html" ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</body>
</div>
<!-- End Row -->
</div>
<!-- End Container -->
</div>
<script type="text/ng-template" id="customTemplate.html">
<a>
<span bind-html-unsafe="match.label | typeaheadHighlight:query" style="width: auto;"></span>
({{match.model.Year}})
<!-- <img ng-src="{{match.model.Poster}}" width="120"> -->
</a>
</script>
<div ng-controller="MyCtrl">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
</section>
Edit
Here's the code that sends data into the MYSQL database:
function addWatchList() {
if (isset($_GET['addtowatchlist'])) {
$name = $_GET['name'];
$conn = dbmysql();
$query ="INSERT INTO watch_list values(null,'{$name}','{$_SESSION['user_id']}','NOW()')";
if (mysqli_query($conn,$query)) {
$last = "SELECT * FROM watch_list WHERE created_by = '{$_SESSION['user_id']}' order by id desc limit 1";
$data = mysqli_query($conn,$last);
while ($row = mysqli_fetch_assoc($data)) {
include "_view.php";
}
}else {
echo "An error occurred. Please try again.";
}
exit;
}
}
I am looking to your code, missing attribute action="somefile.php" and method="get" if you are planning on submitting it using the form, you should put it or if you are planning on submitting your code using javascript you can use <form onsubmit="myFunction()"> That is what you are missing. I am not seeing you addtowatchlist name from your input so that php can catch it to your isset.
The submit button isn't inside the form.
You're missing the </form> tag to end the <form>. But you have </div> that matches the <div> just before </form>, so it's implicitly closing the <form> tag as well. Then you have <input type="submit"> after it. Since the submit button isn't inside the form, and has no form tag associating it with the form, it doesn't know which form it should submit when you click on it.
Try this:
<form class="form-watch-list-create">
<div ng-controller="MyCtrl" class="col-md-4">
<input
required="required"
placeholder="Movie title"
type="text"
class="form-control"
typeahead="item.Title for item in getLocation($viewValue)"
typeahead-loading="loadingLocations"
typeahead-min-length="2"
typeahead-wait-ms="1000"
typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html"
ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</form>
This puts the form around both columns.
Use the method attribute in form tag and end the form tag properly
<form role="form" method="get" class="form-watch-list-create">
<div ng-controller="MyCtrl" class="col-md-4">
<input
required="required"
placeholder="Movie title"
type="text"
class="form-control"
typeahead="item.Title for item in getLocation($viewValue)"
typeahead-loading="loadingLocations"
typeahead-min-length="2"
typeahead-wait-ms="1000"
typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html"
ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</form>

My custom wordpress contact form is redirecting me to search results instead of sending the message

I created a contact form that is placed in the footer. my problem is when the form is submitted, it redirects me to the search results and it says
"No Results Found
The page you requested could not be found. Try refining your search, or use the navigation above to locate the post." I would like to know how can I make this work. THanks!
NOTE: I didn't use any plugin.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
// echo $_POST['email'];
echo "FORM SUBMITTED";
$msg=$_POST['msg'];
$email=$_POST['email'];
$name=$_POST['name'];
$subj=$_POST['action'];
$message=$name.' ('.$email.') :'.$msg;
wp_mail( 'psinc123#designsbyps.com', $subj , $message );
}
?>
<form method="POST" action="#" id="contactForm">
<div class="custom-contact-form">
<div class="first-part"><strong class="hey">Hey PS and Company,</strong><br>
<br>
I am <span id="name" contenteditable="true" class="contenteditable details">enter your name</span> and I would like<br>
<input type="hidden" id="name-field" name="name">
to <span id="action" class="contenteditable details chat-topic" style="cursor: pointer;">chat about a project</span>.
<input type="hidden" id="action-field" name="action">
<div id="submit" class="submit submit-first-part">
<div class="text">Next</div>
</div>
</div>
<div class="second-part">
<span id="question" contenteditable="true" data-content="Enter your message" class="contenteditable details">Enter your message</span><br>
<input type="hidden" id="msg-field" name="msg">
<br>My email address is<br><span id="email" contenteditable="true" data-content="you#email.com" class="contenteditable details">you#email.com</span>
<input type="hidden" id="email-field" name="email">
<div id="send" class="submit submit-second-part">
<div class="text">Send</div>
</div>
<div id="cancel" class="cancel">Cancel</div>
</div>
<div class="last-part">
<div id="message"></div><br><br>
<div id="return" class="cancel">Return</div>
</div>
<div class="overlay open">
<div class="choices-wrapper">
<div class="choices">
<div class="line active">know more about us</div>
<div class="line">chat about a project</div>
<div class="line">ask about a job</div>
<div class="line">say YO!</div>
</div>
</div>
</div>
</div>
</form>
You need this type structure :
function process-form () {
#
# -- form processing actions
#
return (0);
}
?> // end function process_form
.
.
.
name is a reserved term in WordPress. Use another key instead.

input data codeigniter always 0 on database

I am new to CodeIgniter. I found the problem.
I have read on CodeIgniter Form always sends '0' data and Zero (0 ) Database Result in Codeigniter but it did not help.
the problem data in the database is always 0.
my add view code
<form method="post">
<div class="form-inline form-group">
<label for="kegiatan">Kegiatan : </label>
<select class="form-control" id="disableSelect" disabled><option>MPAB</option></select> <!-- inget ganti pake id terakhir -->
</div>
<div class="form-group">
<label for="indikator">Indikator : </label>
<input type="text" name="indikator" id="indikator" placeholder="Indikator" class="form-control"> <!-- kosongin, tapi kalo edit munculin indikator yang di edit -->
</div>
<div class="form-group">
<span class='glyphicon glyphicon-plus'></span> add
</div>
</form>
my controller
public function addIpMpabDb() {
if ($this->form_validation->run()==true){
$tambah = array (
'indikator' =>$this->input->post('indikator'),
'id_jk'=>1
);
$this->tb_det_penilaian->addIndikator($tambah);
redirect("ipmpab");
}
my model
function addIndikator($tambah)
{
$this->db->insert('tb_det_penilaian', $tambah);
}
You don't have a valid submit button in your html code.
Remove your link and use an input type "submit" or a button
Replace :
<div class="form-group">
<a href="<?= base_url() ?>ipmpab/addIpMpabDb/" class="btn btn-success">
<span class='glyphicon glyphicon-plus'></span> add
</a>
</div>
With :
<div class="form-group">
<button type="submit" class="btn btn-success">
<span class='glyphicon glyphicon-plus'></span>add
</button>
</div>
... For exemple
Then add the action attribute to the form :
<form method="post" action="<?= site_url('ipmpab/addIpMpabDb'); ?>">

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