Form isn't picking up the variables to post - php

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

Related

Cannot handle a form in 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.

php can't get the text from a text area

I can't figure out why this doesnt get the value of the textarea. I've been to loads of stackoverflow posts and I can't figure out what's wrong. I've tried getting values from textfields, from dropdown lists etc and they all work I just can't get the textarea to work. I've using _GET instead, still didnt work.
This is the message I get if I don't use the isset function: Notice: Undefined index: descri.
Here's the HTML:
<form role="form" action="saveform.php" method="post" name="eventform">
<div class="form-group">
<label for="descri">Description</label>
<textarea name="descri" form="eventform" style="resize:none"></textarea>
</div>
<button type="submit" class="btn btn-default" id="addform">add</button>
</form>
PHP:
<?php
if(isset($_POST['descri']))
{
echo htmlspecialchars($_POST['descri']);
} else {
echo "DOESNTWORK";
}
?>
Just remove form attribute from textarea:
<textarea name="descri" form="eventform" style="resize:none"></textarea>
Remove form attribute from textarea that is located inside the form:
<form role="form" action="saveform.php" method="post" name="eventform">
<div class="form-group">
<label for="descri">Description</label>
<textarea name="descri" style="resize:none"></textarea>
</div>
<button type="submit" class="btn btn-default" id="addform">add</button>
</form>
You have to use it only when your textarea is outside the form (remember form should have id not just name:
<form role="form" action="saveform.php" method="post" id="eventform">
<div class="form-group">
<label for="descri">Description</label>
</div>
<button type="submit" class="btn btn-default" id="addform">add</button>
</form>
<textarea name="descri" form="eventform" style="resize:none"></textarea>
Remove form="eventform" attribute from your <textarea> element. You do not need to set it as your <form> will post your data.
<form role="form" action="saveform.php" method="post" name="eventform">
<div class="form-group">
<label for="descri">Description</label>
<textarea name="descri" style="resize:none"></textarea>
</div>
<button type="submit" class="btn btn-default" id="addform">add</button>
</form>

How to return php code into html body

I am building a simple form script that collects a users email and returns a PIN.
The input sits in standard HTML below:
<p>
<form class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
I have the following if statement that checks the database to see if the email already exists, and if the user would like a PIN reminder.
if($num_rows != 0) {//if email found in table
?>
Email already registered, would you like a PIN reminder?
<form method="POST" action="">
<input type="submit" name="srSend" value="Click here to get pin reminder" />
<input type="hidden" name="srEmail" value="<?php echo strtolower($email);?>" />
</form>
<?php
exit;
}
At the moment, this returns the result to the user as a new page; how do I put this in the actual HTML of the body page, so it would actually appear below the original form input in a new <p> element?
This is a piece of cake with jquery.post
include the jquery library in your html head and you'll need a short script to get the php content by ajax
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function(){
$('#yourForm').submit(function(e){
e.preventDefault();
var data=$('#yourForm').serialize();
$.post('yourphp.php',data,function(html){
$(body).append(html);
});
});
});
</script>
</head>
<body>
<p>
<form id="yourForm" class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
</body>
You could able to achieve that without ajax too. Simply use a hidden iframe in your main page, then set the target attribute of your form to the iframe as follows:
<form method="post" action="page.php" target="myIframe">
.....
</form>
<p id="theResultP"></p>
<iframe src="#" name="myIframe" style="visibility: hidden"></iframe>
The question now, How could you make the page loaded in the iframe "page.php" to interact with the opener page to update the p. This may be done as follow:
//page.php
<script>
result = parent.document.getElementById('theResultP');
result.innerHtml = "<b>The message you want</b>"
</script>
I dont know if I get what you asking for,but this is my solution :
<p>
<form class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
<?php
if (isset($message)){
<p><?php print $message ?></p>
?>
<?php
}
?>
and in top of your file write something like this :
<?php
if($_post['srSend']){
$message='write your message here';
}
?>

form is sending variables by get method

i got a form.
<form id="site-contact-form">
<div>
<div class="wrapper"><span>Ձեր անունը:</span>
<div class="bg">
<div>
<input type="text" class="input" name="contactname" id="contactname" />
</div>
</div>
</div>
<div class="wrapper"><span>Ձեր E-mail-ը:</span>
<div class="bg">
<div>
<input type="text" class="input" name="email" id="email" />
</div>
</div>
</div>
<div class="textarea_box"><span>Տեկստ:</span>
<div class="bg">
<div>
<textarea cols="1" rows="1" name="message" id="message"></textarea>
</div>
</div>
</div>
<div style="clear:both;"></div>
<button id="sub" name="submit">ուղարկել</button>
</div>
</form>
As you see i got no action on it, no method. But when im clicking on button, its refreshing the page (like when it have action) and adding to URL ?contactname=&email=&message=&submit= ... I never met this problem before, why it is sending variables? I dont have any php on page yet...
Beacause the default method is GET and the URL is the same of the page. Use:
<form method="POST" action="/reactor">
<!-- .... -->
</form>
Actually, the action property is mandatory.
Please correct the code with:
<form id="site-contact-form" method="post">
And the submitted values will not be displayed on the URL.
If you do not specify the form method, it will "get" by default.
This is happening here.
Change the type of button as button
<button id="sub" name="submit" type="button">ուղարկել</button>

Using "action" in Twitter Boostrap classes

I'm a newbie programmer trying to utilize Twitter Bootstrap to build out a concept. I'm using PHP and assigning actions within HTML tags to POST data and essentially take a user through the navigation. This has been pretty straightforward when using forms: i.e. here is a snippet that does work. For example, for this snippet of HTML:
<form action="?viewnotes" method="post">
<input type="hidden" name="id" value="<?php htmlout($note['id']); ?>">
</form>
It successfully triggers the following if statement in my index.php:
if (isset($_GET['viewnotes']))
However, I'm trying to do the same thing in my registration page, using a Twitter Bootstrap class for buttons. Here is the HTML:
<a class="btn btn-primary btn-large" action="?register">Sign Up Free!»</a></p>
The PHP code is:
if (isset($_GET['register']))
{
include 'register.html.php';
}
Clicking on the Sign Up button is not invoking the PHP code. If I hard code the URL it works, but then I have a similar issue on the register.html.php page. HTML on the register page has:
<form class="form-horizontal" action="?storeuser" method="post">
<fieldset>
<legend>It's quick and easy...</legend>
<div class="control-group">
<label class="control-label">First Name</label>
<div class="controls">
<input type="text" name="fname" class="input-xlarge" id="fname">
</div>
</div>
<div class="control-group">
<label class="control-label" name="lname">Last Name</label>
<div class="controls">
<input type="text" name="lname" class="input-xlarge" id="lname">
</div>
</div>
<div class="control-group">
<label class="control-label" name="email">Email Address</label>
<div class="controls">
<input type="text" name="email" class="input-xlarge" id="email">
</div>
</div>
<div class="control-group">
<label class="control-label" name="password">Password</label>
<div class="controls">
<input type="password" name="password" class="input-xlarge" id="password">
</div>
</div>
</fieldset>
<button type="submit" name="action" class="btn btn-primary btn-large">Complete Registration</button>
</form>
However, when clicking on the button, the index file does not trigger the following, which would store the fields into the DB.
if (isset($_GET['storeuser']))
If I hardcode the URL to register.html.php, then the resulting URL looks like localhost/register.html.php?storeuser instead of localhost/?storeuser. I'm not sure if that's affecting the behavior here.
Thank you for the help!
I think you're approaching this the wrong way, and it's not Twitter Bootstrap's fault.
Usually, you'd use POST, not GET, to handle user registrations. Your form would look like this:
<form action="register.php" method="post">
<!-- your form -->
<fieldset>
<input type="submit" name="register" value="Register" class="btn btn-primary" />
</fieldset>
</form>
You can then build register.php as follows:
<?php
if (isset($_POST['register'])) {
// handle user registration
}
// display form
?>
This will display the form when the user visits register.php, but will try and process the user registration first if the form's been POSTed.
try passing a value with your GET variable
<form action="?viewnotes=1" method="post">

Categories