Not Getting the desired Output in PHP - php

My code looks completely correct.I just copied it from New Boston php tutorial,its working there. But its not working properly here. Every time after the submission else block get executed and it produces the output "Please fill the form". If I filled all the fields than if block should get executed, and it should print the 'contact_name', 'contact_email', 'contact_text', but its not.I am writing this code in netbeans.
here is the code:
<?php
if(isset($_POST['contact_name']) && isset($_POST['contact_email'])
isset($_POST['contact_text']))
{
echo $contact_name=$_POST['contact_name'];
echo $contact_email=$_POST['contact_email'];
echo $contact_text=$_POST['contact_text'];
}
else
{
echo 'Please fill the form';
}
?>
<form action="index.php" method="POST">
Name:<br><input type="text" name="=contact_name"><br><br>
Email address:<br><input type="text" name="contact_email"><br><br>
Message:<br>
<textarea name="contact_text" rows="6" cols="30"></textarea><br><br>
<input type="submit" value="Send">
</form>

Replace
Name:<br><input type="text" name="=contact_name"><br><br>
With
Name:<br><input type="text" name="contact_name"><br><br>

You have an error in your contact_name field:
Change this:<input type="text" name="=contact_name">
to this: <input type="text" name="contact_name">

There is an error of name="=contact_name"
Remove extra =
name="contact_name"

Related

php multipage input value session variable print

please correct me here.
i have created multi page form where i want to pass data from each pages to final pages and then submit those on email. first one is apply.php, there are many input fields, but i have listed some of those, here when some enters passport number in passport field, i want this to be passed in everypage of the form and print this at couple of places on each page. here getting some issues when passing some of these fields.
this is first page ( apply.php )
<?php
// Start the session
session_start();
?>
<form name="search_form" method="post" onSubmit="return chk();" action="apply2.php">
<input name="passportno" id="passportno" type="text" maxlength="20" placeholder="Enter Passport No." size="43" >
<input name="birthdate" type="date" class="textBoxDashed" size="43" id="birthdate" datepicker="true" datepicker_min="01/01/1900" datepicker_max="21/11/2017" maxlength="10" datepicker_format="DD/MM/YYYY" isdatepicker="true" value="">
<input name="button1" type="submit" value="Continue">
this is apply2.php . here there is some issues, i am not able to find, as you can see below codes, i am able to print date of birth but not able to print passport no ( input from form1 ). Please correct where i am wrong here.
<?php
session_start();
$msg="";
////include("connect.php");
if (isset($_POST['button1']))
{
extract($_POST);
$code=strtolower($_POST['captcha_code']);
$sess=strtolower($_SESSION["code"]);
if ($sess==$code)
{
$appid=time().rand();
$result=mysqli_query($con,"select *from registration where email='$email'");
if (mysqli_fetch_row($result)>0)
{
?>
<script>
alert("This email is already exist");
</script>
<?php
}
else
{
$query="insert into registration values('$appid','$passportno','$birthdate','$email')";
if (mysqli_query($con,$query))
$msg="Data saved Successfully.Please note down the Temporary Application ID $appid";
else
echo "not inserted".mysqli_error($con);
if (!isset($_SESSION["appid"]))
{
$_SESSION["appid"]=$appid;
}
}
}
else
{
?>
<?php
}
}
?>
<form name="OnlineForm" method="post" onsubmit="return OnSubmitForm();" action="apply3.php">
<input name="applid" id="applid" value="<?php echo $_SESSION["appid"];?>">
<input type="hidden" name="birthdate" value="<?php echo $birthdate;?>"><b><?php echo $birthdate;?>
<input name="passportno" type="text" class="textBoxDashed" id="passportno" value="" size="43" maxlength="14" value="<?php echo $passportno;?>">
input name="sc" type="submit" class="btn btn-primary" id="continue" value="Save and Continue" onclick="document.pressed=this.name">
Don't use extract. Also do some checking to see if the data is set. As for not getting the the data try $_POST['passportno'] and if you want to pull the values and put them back into the input boxes simply use <?php echo isset($_POST['passportno'])?$_POST['passportno']:'' ?> to return nothing if it is not defined.
Also you need to do add some protection to your inputs.
You can add protection by using $passportno = mysqli_real_escape_string($con, $passportno);

PHP - cannot get value when input exists

I'm trying to use POST to get values from a form. I moved the part out to a test page:
<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" value="enter something" />
</form>
// following that
<?php
if (isset($_POST["subject"]))
echo $_POST["subject"];
else
echo "input is not set";
?>
The echo is always "input is not set" regardless I set the value of input or not. And the tag "subject" does exist. This confused me. Why can't I get the value?
You should seperate your HTML form from your $_POST processing. This means that you provide the user a page which contains your posted form and then submit of this form is posted to a (different) page.
For example your form is (on form.php):
<form id="form" action="process_form.php" method="post">
<input type="text" name="subject" id="subject" value="" />
<input type="submit" value="Submit">
</form>
Then you create a file process_form.php:
<?php
if (isset($_POST['subject'])) {
echo $_POST['subject'];
}
else {
echo 'input is not set.';
}
?>

How to stop submit being shown in the address bar?

How do i stop the submit being shown in the search bar? am i doing something wrong? if i use post method it works fine, but recently i've tried using the get method and it prints both submit and the textbox text in the bar, i tried removing the name="submit" and it didn't do anything when i clicked it. SO how do i stop submit being shown in the address bar?
at the moment it give me $submit=submit, :(
ty all.
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
<?php
if(isset($_GET['submit']))
{
if(!empty($_GET['website']))
{
//do stuffs here
}
else
{
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>
Just use:
!empty($_GET)
In your first if statement
So your code should look something like this:
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit">
</form>
<?php
if(!empty($_GET)) {
if(!empty($_GET['website'])) {
//do stuffs here
} else {
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>
To remove the variables from the URL bar, you need to use the POST method instead of the GET method.
So, this form code should work for you:
<form action="" method="POST">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
You'll also need to alter your PHP code to use $_POST instead of $_GET.
The downside to this is that it won't show the submitted website in the URL bar, meaning users can't bookmark a submitted form. However, this may also be intended.
Alternative Solution
You can remove the name from the submit, and use isset($_GET) to test whether the form was submitted instead.
Here is an example of how this could be done:
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit">
</form>
<?php
if(isset($_GET) && count($_GET) > 0) {
if(!empty($_GET['website']))
{
//do stuffs here
} else {
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>

Post Form DATA to another page after PHP Validation

I have this issue with my validation and posting the data to another page.
Here is my form:
Signup.php
<form id="regForm" action="<?php echo htmlspecialchars($_SERVER["submit.php"]);?>" method="post" name="regForm">
<label for="fname">First Name:</label><input name="fname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['fname'])){echo $_POST['fname'];}?>"/><br/>
<label for="mdname">Middle initial:</label><input name="mdname" type="text" size="10" maxlength="35" value="<?php if(isset($_POST['mdname'])){echo $_POST['mdname'];}?>"/><br/>
<label for="lname">Last Name:</label><input name="lname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['lname'])){echo $_POST['lname'];}?>"/><br/>
<br/>
<label> </label><input type="submit" name="Signup" class="formButton" value="Signup" /></form>
And here is my submit.php which will validate the signup.html input
submit.php
function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}
Now, my issue is this, in my "submit.php" file, I want to know what codes to put after the form fields validation that would enable me post the input data to another page because, I plan making it a two-page signup form. Let's say my next page is signup-2.html
how do I post the data after validation to the next page? I know how to retrieve the posted data on the next page like using Session or Echo the $_POST data but, my main issue is....how do I make the form post the data after the validation messages in my submit.php file?
use header :
header("Location: your page url?fname=$_POST['fname']&lname=$_POST['lname']");
but before this do not echo or print anything otherwise it won't redirect to that page.
you can use the data on destination page like this:
$_GET['fname']
example:
submit.php
function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}
header('Location:download.php?fname='.$_POST['fname']."&lname=".$_POST['lname']);
view.php
<html>
<body>
<form action="submit.php" method="post">
<input type='text' id="fname" name="fname"/>
<input type='text' id="lname" name="lname"/>
<input type="submit" id="button" value="submit"/>
</form>
</body>
</html>
download.php
<?php
echo "First Name........".$_GET['fname'];
put these three file in same directory and run view.php. you will be ok.

HTML submit forms using PHP

I am learning PHP now, so pardon my silly question which I am not able to resolve.
I have created a simple web form where in I display the values entered by a user.
function submitform()
{
document.forms["myForm"].submit();
} // in head of html
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" onclick="submitForm()" />
</form>
and res.php contains:
foreach($_POST as $field => $value)
{
echo "$field = $value";
}
When I click on the submit button, I just get a blank page without any values from the form. Can anyone please let me know what am I missing?
There's no need for the javascript. This should do:
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" />
</form>
Let's start with fixing errors:
JavaScript is case-sensitive. I see that your function name is submitform and the form's onclick calls submitForm.
The javascript is not really necessary from what you've shown us, I would try this on a single php page and see if it works:
Create a test.php file for test purpose:
<?php
if($_POST){
foreach($_POST as $key=>$value){
echo "$key: $value<br />";
}
}else{
<form action="test.php" method="post">
<input type="text" value="1" name="name1" />
<input type="text" value="2" name="name2" />
<input type="submit" value="submit" name="Submit" />
</form>
}
?>
If it does work, slowly work your way into your current form setup to see what is breaking it. If it doesn't work, there's something larger at play.
There are 2 things you should do now.
Remove the JavaScript function to submit the form. It's not required (or necessary). The default behavior of a submit button is to... well... submit. You don't need to help it with JavaScript.
Enable error display by using error_reporting(E_ALL).
After you do both things, you should be able to debug and assess the problem much more easily.
Put your php code inside php tags!
<?php
foreach($_POST as $field => $value)
{
echo $field ." = ." $value.'<br />';
}
?>
If you do
<?php
print_r($_POST);
?>
what do you get?
If this still doesn't work, does your server parse php?
Create the file test.php and access it directly http://localhost/test.php or whatever your URL is
<?php
echo 'Hello';
?>
if this doesn't work..it's a whole diferent problem
You can submit an HTML form using PHP with fsubmit library.
Example:
require_once 'fsubmit.php';
$html = "<form action ='res.php' method='post'><input type='text' name='name'/></form>";
$form = new Fsubmit();
$form->url = 'http://submit_url_here.com';
$form->html = $html;
$form->params = ['name'=>'kokainom'];
$response = $form->submit();
echo $response['content'];

Categories