If i use this code to take as input a number it works fine
<form method = "post">
Hours: <input type="text" name="myvar">
</form>
<?php
require 'connect.php';
if (isset($_POST["myvar"])) {
But the following code to get 2 numbers doesn't go pass if. How can i make this work?
<form method = "post">
Hours: <input type="text" name="myvar">
</form>
<form method = "post">
Minutes: <input type="text" name="myvar2">
</form>
<?php
require 'connect.php';
if (isset($_POST["myvar"]) && isset($_POST["myvar2"])) {
You just need to use one form for both fields as:
<form method="post">
Hours: <input type="text" name="myvar">
Minutes: <input type="text" name="myvar2">
<input type="submit" name="submit">
</form>
And in PHP you can use !empty()
for form input.
<?
if (!empty($_POST["myvar"]) && !empty($_POST["myvar2"])) {
...
?>
Second solution, if you still want to use isset()
than also add !empty() for checking as:
<?
if (isset($_POST["myvar"]) && !empty($_POST["myvar"]) &&
(isset($_POST["myvar2"]) && !empty($_POST["myvar2"]))) {
echo "<pre>";
print_r($_POST);
}
?>
Use only one form. Furthermore having more than one input requires and input with the type of submit
or image
so that you can submit the form.
<form method = "post">
Hours: <input type="text" name="myvar"/>
Minutes: <input type="text" name="myvar2"/>
<input type="submit"/>
</form>
I am assuming that you dont want to show the submit button ,the thing that you can do is to push the button away from screen using css.
<form method = "post">
Hours: <input type="text" name="myvar"/>
Minutes: <input type="text" name="myvar2"/>
<input type="submit"
style="position: absolute; left: -9999px; width: 1px; height: 1px;"
tabindex="-1" />
</form>
You are using 2 forms element .. make it one as
<form method = "post">
Hours: <input type="text" name="myvar">
Minutes: <input type="text" name="myvar2">
</form>
You are using 2 forms element
. Both inputs should be in same form
<form method = "post">
Hours: <input type="text" name="myvar">
Minutes: <input type="text" name="myvar2">
</form>
As others already mentioned, you should indeed have both input fields in the same form tags. The reason behind it, is this:
A form is used to contain data that is send to the server once you submit it. If you have multiple forms, only the data that is inside the form you submit, will be sent.
Therefore, when you submit one of the forms, either myvar
or myvar2
will be sent to the server. Not both.
So, if you check if they are both set, it will fail, because they are not.
Putting both input fields in the same form will solve this:
<form method="post">
Hours: <input type="text" name="myvar">
Minutes: <input type="text" name="myvar2">
</form>
You can use two form but action should be same then you can get two variable values using $_POST
method.
Hours:
Minutes: <input type="text" name="myvar2" value="">
<input type="submit" name="submit" value="submit"/>
<?php
if (isset($_POST['submit']) && !empty($_POST['submit']))
{
echo $_POST['myvar'];
echo "<br/>";
echo $_POST['myvar2'];
}
?>