I am using this code in my *.php file
$check = $_POST['dati'];
if (strlen($check) != 0) {
// calculations
}
else {
echo "contact me at <a href='mailto:myemail'></a>";
}
The dati inside that POST is the name attribute of an input field. If the input has a length = 0, that echo must be displayed.
I have the same code in another part of my server and it works perfectly. I'm having troubles here because when the length of dati is 0, the script makes the calculations instead of showing the echo.
<input name="dati" id="dati" style="width:310px" type="text">
This is the code of the input. Any ideas?
You code works well as you can see here: http://codepad.org/fCvlokOJ and here http://codepad.org/t2TvFozt
<?php
$_POST['dati']= "text";
if (strlen($_POST['dati']) != 0) {
echo" calculations";
}
else {
echo "contact me at <a href='mailto:myemail'></a>";
}
In the following example, if field is left blank, echo'ed message is "sorry".
If text entered, then Email link appears. TESTED
<?php
if(isset($_POST['submit']))
$check = $_POST['dati'];
if (strlen($check) == 0) {
echo "Sorry";
}
else {
echo "Contact me at <a href='mailto:myemail'>LINK</a>";
}
?>
FORM
<form method="post" action="your_handler.php">
<input name="dati" id="dati" style="width:310px" type="text">
<input type="submit" name="submit" value="Submit">
</form>
Related
I am trying to get the user input and verify if the answer is correct using an if and statement. How do I prompt the user for input within the same webpage? This is the code I have so far.
<?php
$answer = //user input
if ($answer = "4") {
echo " “Correct!!!! Congratulations!";
} else {
echo "Your answer was blank but the correct answer is 4.";
}
?>
This is the full form view to let you know how to perform the basic form submit and get values and displaying the result
<?php
if(isset($_POST['submit']))
{
$answer=$_POST['answer'];
if ($answer == "4") {
echo " “Correct!!!! Congratulations!";
} else {
echo "Your answer was blank or wrong input but the correct answer is 4.";
}
}
?>
<html>
<body>
<form method="post" action="index.php">
<input type="number" name="answer">
<input type="submit" name="submit" value="Submit">
</form>
I'm relatively new to the web development scene and have been assigned with creating a website capable of logging calls.
I have used a HTML form to achieve this - I have made many of these in the past, but have never encountered this issue before.
My page contains 3 buttons: one to log a call, one to forward a call, and one to view all call logs. I achieve this by using an onclick method in the buttons:
<button type=submit value=log onclick="window.location.href='reception.php?log=1';">Log a call.</button>
And then using PHP GET to display the appropriate content on the rest of the page.
try {
$log = $_GET["log"];
}
catch(Exception $ex) {
die();
}
if($log) {
?>
// create form
This works perfectly, and I have created my form as below.
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" name="log">
<p>
<label>Call date: </label>
<input type="date" name="date" value=<?php echo date("Y-m-d"); ?>>
<span class="error"><sup> <?php echo $dateErr; ?></sup></span>
</p>
<br>
<p>
<label>Does the client have a contract?</label>
<select name="contract">
<option value="default" selected disabled>Please select...</option>
<option value="true">Yes</option>
<option value="false">No</option>
</select>
<span class="error"><sup> <?php echo $contractErr; ?></sup></span>
</p>
<br>
<p>
<label>Client forename:</label>
<input type=text name="fname" <?php if(isset($fname)) echo "value='".$fname."'";?>>
<span class="error"><sup> <?php echo $fnameErr; ?></sup></span>
</p>
<br>
<p>
<label>Client surname:</label>
<input type=text name="sname">
<span class="error"><sup> <?php echo $snameErr; ?></sup></span>
</p>
<br>
<p>
<label style="position: relative; top: -135px;">Client enquiry:</label>
<textarea name=enq style="font-size: 14px; height: 150px; width: 300px;"></textarea>
<span class="error"><sup> <?php echo $enqErr; ?></sup></span>
</p>
<br>
<p>
<button type=submit name=submit value=submit style="position:relative; right: -115px; height:40px; width: 100px;">Submit</button>
</p>
It works just fine. However, the issue comes when submitting the form: for some reason, all PHP variables that I define come up empty when submit is clicked, meaning that any auto-completion / error messages do not show up. As you can see in my form code, as a test I made it autofill the "fname" field if the user has already set it, but it does not work.
Here is my PHP for validation (it is not complete yet, I just wanted to do all of the 'isset' checks first):
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST["submit"])) {
$go = true;
$query = "INSERT INTO callLog VALUES(NULL, '";
if(empty($_POST["date"])) {
$dateErr = "* Please enter a date.";
$go = false;
}
else {
$query .= date('Y-m-d', strtotime($_POST["date"]))."', ";
}
if(empty($_POST["contract"])) {
$contractErr = "* Please select 'Yes' or 'No'.";
$go = false;
}
else {
$contract = test_input($_POST["contract"]);
if($contract == "Yes") {
$contract = "TRUE";
}
else {
$contract = "FALSE";
}
$query .= $contract.", '";
}
if(empty($_POST["fname"])) {
$fnameErr = "* Please enter a forename.";
$go = false;
}
else {
$fname = test_input($_POST["fname"]);
$query .= $fname."', '";
}
if(empty($_POST["sname"])) {
$snameErr = "* Please enter a surname.";
$go = false;
}
else {
$sname = test_input($_POST["sname"]);
$query .= $fname."', '";
}
if(empty($_POST["enq"])) {
$enqErr = "* Please enter an enquiry.";
$go = false;
}
else {
$query .= $enq."');";
}
if($go) {
$conn->query($query);
header('Location: reception.php');
}
?><script>
$(document).ready(function() {
window.location.href='reception.php?log=1';
});
</script><?php
}
}
The JS at the end is simply so that the page redirects to the call log form page once the data has been submitted, rather than having to click the 'call log' button again.
I'm completely at a loss as to why this doesn't work. Aside from the 'GET' method to display the page, I've done everything as I have in the past which has worked fine. Is it the 'GET' method interfering, or am I missing something?
I have seen that potentially trying something such as Ajax to handle the submission to see if it will work is a possibility, but I am not too familiar with JQuery (knowing only the basics) and do not know how to work with Ajax.
Thanks!
You're highjacking the form submission. You're onclick="window.location.href='reception.php?log=1';" just makes a GET request to that page, not a POST to your form action.
I'm very new to PHP and I've cobbled this together from some other answers on here. Can anyone show me how to get the $errMsg to display? At present, a blank or incorrect name leads to a blank page. Is this because the form isn't being displayed again? If so, how should I go about 'reloading' the form with the error message?
<?php
$name = "Fred";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST["name"])) {
if ($_POST["name"] == $name) {
include("welcomeFred.php");
}
else {
$errMsg = "Incorrect name";
}
}
else {
$errMsg = "Name required";
}
}
else { ?>
<html>
...
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" name="name" required>
<span><?php echo $errMsg;?></span>
<input type="submit" value="Submit">
</form>
...
</html>
<?php } ?>
You shouldn't put the rendering of the form in the else of your if structure. This is the reason your form isn't loaded when you submit the form.
Remove the else { ?> and <?php } ?> at the end of your file and it should work fine.
This link leads to where my code is ran.
http://erobi022.pairserver.com/phpvalidate1.php
<!DOC TYPE html>
<html>
<body>
This is a simple form
<form method="post" action="send_phpvalidate1.php">
Please enter your first name: <input type="text" name="First"></p>
Please enter your last name: <input type="text" name="Last"></p>
<button type="submit">Submit</button>
</form>
</body>
</html>
Once the submit button is hit, all data is sent and posted here. I can get first and last name to output fine, but if i leave them blank it will only say that i left my first name field blank.
http://erobi022.pairserver.com/send_phpvalidate1.php
<!DOCTYPE html>
<html>
<body>
Welcome,
</P>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name1 = $_POST["First"];
if (empty($name1)) {
echo "You've entered nothing for first name";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your first name is $name1 ";
}
}
echo "<br>";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name2 = $_POST["Last"];
if (empty($name2)) {
echo "You've entered nothing for last name";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your last name is $name2 ";
}
}
?>
<?php // can have multiple php sections
echo "<a href='phpvalidate1.php'>Return to form</a></p>";
//have to use a simple qoute within html to make it work
?> </p>
Return to home page
</body>
</html>
You can just use that using HTML 5, simple
<!DOC TYPE html>
<html>
<body>
This is a simple form
<form method="post" action="send_phpvalidate1.php">
Please enter your first name: <input required="required" type="text" name="First"></p>
Please enter your last name: <input required="required" type="text" name="Last"></p>
<button type="submit">Submit</button>
</form>
</body>
</html>
No need to use two big ifs, just replace your PHP code with this :
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name1 = $_POST["First"];
$name2 = $_POST["Last"];
if (empty($name1) || empty($name2)) {
echo "Please complete both the fields.";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your name is $name1 $name2";
}
}
?>
First, whenever there is a die() statement,only the codes that appear before that run, the ones which come after don't get to run(in relation to your code.)
you can also trim your codes down this way and it will still work
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name1 = $_POST["First"];
$name2 = $_POST["Last"];
if (empty($name1) || empty($name2)) {
echo "Please complete both the fields.";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your name is $name1 $name2";
}
}
?>
I have this piece of code and I want to show result in the third input box not in the input box and hole page and i want to know if i can a $_GET['$vairbles'] whiten functions
NOTE : I’m beginner in php programming
Here is my code :
<?php
$email = "mail#somedomain.com";
if (isset($_GET['txt1']) and isset($_GET['txt2'])) if (!empty($_GET['txt1']) and!empty($_GET['txt2'])) {
$tex1 = $_GET['txt1'];
$tex2 = $_GET['txt2'];
echo "They are all filled.<br>";
} else {
echo "Please fill in first and second Fields";
}
shwor();
Function shwor() {
global $email;
global $tex1;
global $tex2;
$len1 = strlen($tex1);
$len2 = strlen($tex2);
if ($len1 and $len2 > 0) if ($tex1 == $tex2) echo "matched ";
else echo "Does not match";
}
?>
<form action:"email.php" method="GET">
<input 1 type="text" name="txt1"><br><br>
<input 2 type="text" name="txt2"><br><br>
<input 3 type="text" name="Result" value = "<?php shwor();?>"><br><br>
<input type="submit" value="Check matches"><br>
</form>
everything works, just misspelled in this line:
<form action:"email.php" method="GET">
should be:
<form action="email.php" method="GET">
..and remember to name your file email.php that the data will be sent to the file itself ;-)