Good morning,
I have a submit form,s code and it works perfectly. But when i add more inputs or radio buttons it stops working. I'm a front-end developer and i'm not very good at back-end development. Can anybody please help me to sort it out.
Thanks
<html>
<head></head>
<body>
<form action="invia.php" method="POST">
Nome: <input type="text" name="nome">
<br><br>
Email: <input type="email" name="email">
<br><br>
Messaggio:
<br>
<textarea name="message" rows="10" cols="50" ></textarea>
<br><br>
<input type="submit" value="Invia">
</form>
</body>
</html>
<?php
$client = $_POST['nome'];
$email = $_POST['email'];
$message = $_POST['message'];
mail("someone#gmail.com","Contact from the site",$message,"From: $email\r\n");
?>
If we say we have this:
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
Then only the last value will be taken because names overlap each other.
Related
I have a question .
This is the scope of the question :
Prepare a registration form on php and while submiting the data it will go to next page .Give link to a third page and Use sessions to print the collected data on this page.
I'm new to it so not really sure how to continue but I have prepared a form .It is as follows
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<h2> Form Example</h2>
<form method="post" action="">
Name: <input type="text" name="name" value="">
<br><br>
Address: <input type="text" name="address" value="">
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Kindly guide me on how to continue further
Use session variable for this.
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<h2> Form Example</h2>
//add the redirect page
<form method="post" action="redirect_page.php">
Name: <input type="text" name="name" value="">
<br><br>
Address: <input type="text" name="address" value="">
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
the redirect_page.php in the action should have the following code.
<?php session_start();
//submitted button created session variabale
if( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
$_SESSION['submit']['name'] = $_POST['name'];
$_SESSION['submit']['comment'] = $_POST['comment'];
$_SESSION['submit']['gender'] = $_POST['gender'];
}
foreach ($_SESSION['submit'] as $key => $value)
{
echo $key. ' = ' .$value.'</br>';
}
?>
//third Page Link
Third Page
the third page named thirdpage.php need only to have the fowllowing code. Because session submit already exists thanks to redirect_page.php has create for us!
<?php session_start();
foreach ($_SESSION['submit'] as $key => $value)
{
echo $key. ' = ' .$value.'</br>';
}
?>
I am just tottaly newbie with PHP and I am now learning how to combine PHP with HTML form. I wanted it in one file, so I tried this:
<?php
if(isset($_POST['button'])){ //check if form was submitted
$pohlavie = $_POST['gender']; //get input text
$plat = $_POST['salary']; //get input text
$plat = "Your gender is ".$pohlavie." and your salary is ".$plat;
}
?>
<center><h1>TAXES</h1></center>
<form action="" method="post">
Name: <input type="text" name="name"><br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="male"> Male<br>
Salary: <input type="number" name="salary"><br>
<button type="submit" name="button" formmethod="post">Calculate DPH</button>
</form>
Unfortunately, it's literally doing nothing after submitting. Can you please help me a little bit?
Trying the using the following line in place of your current form line:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">`
echo $plat = "Your gender is ".$pohlavie." and your salary is ".$plat;
here is:
<?php
if(isset($_POST['button'])){ //check if form was submitted
$pohlavie = $_POST['gender']; //get input text
$plat = $_POST['salary']; //get input text
echo "Your gender is ".$pohlavie." and your salary is ".$plat;
}
?>
<center><h1>TAXES</h1></center>
<form action="" method="post">
Name: <input type="text" name="name"><br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="male"> Male<br>
Salary: <input type="number" name="salary"><br>
<button type="submit" name="button" formmethod="post">Calculate DPH</button>
</form>
Always follow best coding practices and use recommended features. Here the code with little modifications:
<?php
if (filter_has_var(INPUT_POST, "button")) { //check if form was submitted
$pohlavie = filter_input(INPUT_POST, 'gender'); //get input text
$salary = filter_input(INPUT_POST, 'salary');
echo $plat = "Your gender is ".$pohlavie." and your salary is ".$salary;
}
?>
<center><h1>TAXES</h1></center>
<form action="" method="post">
Name: <input type="text" name="name"><br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="male"> Male<br>
Salary: <input type="number" name="salary"><br>
<button type="submit" name="button" formmethod="post">Calculate DPH</button>
</form>
You can also use different type of sanitize filters based on your needs. See here: http://php.net/manual/en/filter.filters.sanitize.php
Also see this post to get more knowledge regarding filter_input:
When to use filter_input()
Hope it will help you to learn and follow best practices in PHP.
I want to send an email from the browser, but my PHP knowledge and experience are not enough.
So I have a html form
<form>
<textarea value="Message" required></textarea>
<input type="text" value="Name" required>
<input type="text" value="Email" required>
<input type="text" value="subject" required>
<input type="reset" value="Reset" >
<input type="submit" value="Submit">
</form>
My question here is how to fill this php code so I can get data from the html form and then send the email.
$to = "my_email#example.com";
$name =
$message =
$from =
$headers =
mail($to,$name,$subject,$message,$headers);
You have to add an action and a method (POST or GET) in your form
<form action="yourpage.php" method="POST">
After that add a name attribute at all your input :
<input type="text" value="Name" name="name" required>
<input type="text" value="Email" name="mail" required>
In yourpage.php
Here the method was POST so :
$_POST['name']; //Here get the posted value in input named 'name'
$_POST['mail'];
You need to set the action in the form, and get the $_POST in PHP code, here is an example:
<form action="test.php">
<textarea value="Message" required></textarea>
<input type="text" name="Name" value="Name" required>
<input type="text" name="Email" value="Email" required>
<input type="text" name="subject" value="subject" required>
<input type="reset" value="Reset" >
<input type="submit" value="Submit">
</form>
//test.php file
<?php
$to = "my_email#example.com";
$name = $_POST['Name'];
$message = $_POST['Message'];
$from = 'test#test.com';
$headers = 'your headers';
mail($to,$name,$subject,$message,$headers);
?>
I have a HTML form with radio buttons, option boxes (drop down boxes), checkboxes, text fields and so on. I have the form pointed to another file called send.php to email the form on but how would I do this with the tickboxes and radio buttons and input text in between each answer? I would kind of like to format it like this:
Welcome: {name}
Your age group it between: {radio button with age groups}
And so on. I can't give you the actual code as it is private but I can give this instead which uses the kind of code and format:
<form action="send.php">
<input type="radio" name="AgeGroup" value="AgeGroup1"> 0-18<br>
<input type="radio" name="AgeGroup" value="AgeGroup2"> 19-29<br>
<input type="radio" name="period" value="AgeGroup3"> 30-39<br>
<input type="radio" name="period" value="AgeGroup4"> 40-49<br>
<input type="radio" name="period" value="AgeGroup5"> 50+<br>
<br><br><br><br>
<select name="Country">
<option value ="UK">United Kingdom</option>
<option value ="USA">United States</option>
</option></select>
<br><br><br><br>
<input type="text" name="PostCode" size="5">
<br><br><br><br>
<input type="text" name="HouseNumber" size="5">
<br><br><br><br>
<textarea id="Family" class="input" name="FamilyNumber" rows="10" cols="60"></textarea>
<br><br><br><br>
<input type="checkbox" name="Delievery" value="NextDay"> Next Day Delievery
<br>
<input type="checkbox" name="Delievery" value="TwoToFive"> 2-5 Day
<br>
<input type="checkbox" name="Outcome" value="Dismissed"> Dismissed
<br><br><br><br><br><br>
<center><button id="Send" type="submit" style="height:25px; width:100px; background-color: grey">Send</button></center>
</form>
Sorry it's so random. I ran out of ideas! Also sorry for my coding abilities, I don't normally do HTML!
Thanks.
I'm hoping this will help...if I'm understanding your question correctly.
Make sure to add a method to your form tag. For example:
<form action="send.php" method="post">. In send.php, you want to grab your variables by name attribute, for example:
$name = $_POST['Name'];
$ageGroup = $_POST['AgeGroup'];
And then you want to build out your email. PHP allows variables to be parsed in double quote strings, which can help you build your message the way you want it. (Reference)
$to = "person#example.com";
$subject = "Subject goes here";
$message = "Welcome: $name. Your age group is between: $ageGroup";
//note: headers are optional
$headers = "From: you#example.com" . "\r\n" . "CC: anotherperson#example.com";
mail($to, $subject, $message, $headers);
This is just a simple example, but this might be able to get you started.
you need to add method="POST" attribute to tag.
then, in send.php to read values, for example PostCode or HouseNumber:
echo "Post code: ".$_POST["PostCode"]."<br />";
echo "House number: "$_POST["HouseNumber"]";
for age:
if(isset($_POST["AgeGroup"]) {
echo "Age group: ".$_POST["AgeGroup"];
}
if(isset($_POST["period"]) {
echo "Age group: ".$_POST["period"];
}
for more info go to manual: http://php.net/manual/en/reserved.variables.post.php
This way you can group the radio buttons into one name, and you can get the values in the php as $_POST['name']
<form action="send.php" method="post">
<input type="radio" name="AgeGroup" value="0-18"> 0-18<br>
<input type="radio" name="AgeGroup" value="19-29"> 19-29<br>
<input type="radio" name="AgeGroup" value="30-39"> 30-39<br>
<input type="radio" name="AgeGroup" value="40-49"> 40-49<br>
<input type="radio" name="AgeGroup" value="50+"> 50+<br>
<select name="Country">
<option value ="UK">United Kingdom</option>
<option value ="USA">United States</option>
</option></select>
<input type="text" name="PostCode" size="5">
<input type="text" name="HouseNumber" size="5">
<textarea id="Family" class="input" name="FamilyNumber" rows="10" cols="60"></textarea>
<input type="checkbox" name="Delievery" value="NextDay"> Next Day Delievery
<br>
<input type="checkbox" name="Delievery" value="TwoToFive"> 2-5 Day
<br>
<input type="checkbox" name="Outcome" value="Dismissed"> Dismissed
<center><button id="Send" type="submit" style="height:25px; width:100px; background-color: grey">Send</button></center>
</form>
When I submit this form it downloads the php script it refers to:
<form action="insertcustomer.php" method="post" class="myform">
<p>
Name: <input type="text" name="name" size="20"/>
</p>
<p>
Address: <input type="text" name="addr" size="40"/></p>
<p>
City: <input type="text" name="city" size="20"/>
State: <input type="text" name="state" size="3"/>
Phone: <input type="text" name="phone" size="15"/>
</p>
<p>
Account Balance: <input type="text" name="acct_balance" size="13"/>
</p>
<p class="bttn">
<input class="b" type="submit" name="query" value="Insert Customer"/>
<input class="b" type="reset" name="reset" value="Clear Form"/>
</p>
</form>
Insertcustomer.php:
$name = $_POST['name'];
$addr = $_POST['address'];
$city = $_POST['ccity'];
$state = $_POST['cstate'];
$phone = $_POST['phone'];
$acct_balance = $_POST['acct_balance'];
Is there something wrong with the code? I have followed all steps mentioned in this article, but every time I click on the submit button, it downloads the php file. I apologize in advance as I am fairly new to php.
Make sure your code is wrapped in <?php and ?>
Does your server support PHP?
Is your server configured properly?