<div class = 'buttons'>
<button type="submit" class="regular" name="save">
<img src="elephant.png" alt=""/>
Memory
</button>
</div>
This is the code for a submit button
however when i try validate the form using php, the button does not seem to be submitting
<?php if( isset($_POST['save']) && $_POST['save'])
{
$submit = $_POST['save'];
echo "lol";
}
else
{
echo "lola";
}
Submit buttons only work in forms. Try to wrap the whole thing in a form tag and try again.
Your submit button doesn't have any value to send when posting the form. The <button> element does not send its element's content as its submit value. You still have to add a value attribute to the element in order for it to have a submitted value. Since you don't, it's sending an empty string, which is causing your conditional statement to fail:
if( isset($_POST['save']) && $_POST['save'])
Since $_POST['save'] is empty, that second part returns false and thus it goes to your else block.
Here is the code that you want:
<form name="myForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div class="buttons">
<button type="submit" class="regular" name="save" value="Save">
<img src="elephant.png" alt="Elephant" />
Memory
</button>
</div>
</form>
As written already your code lacks the wrapping. While submit button as a button might work without the form, then catching the _POST data surely doesnt.
Also, your solution for image-button could be better, maybe something like this:
<style type="text/css">
input[type="submit"] {background: url('elephant.png') right no-repeat; padding: 10px; padding-left: 30px; font-size: 14px; font-weight: bold;} /* or call this .regular */
</style>
<?
if ($_POST['save']) { // the trigger
echo '<h1>Cooking with fire now!</h1>';
}
?>
<form method="POST" action="">
<div class="buttons">
<input type="submit" name="save" value="Memory" class="regular" />
</div>
</form>
Note that class = "buttons" with the spaces, is incorrect syntax!
You should include your code within <form method="post" action="path/to/your/php/processing/file"> and </form> tags
Replace your button code with <input type="submit" class="regular" name="save" />
Remove isset($_POST['save']) && part from condition of if as you don't have set value for your button.
Related
Can anybody tell me why if "Append" or "AppendTo" or "After" or "prepend" an input field inside a div which is nested inside the form tag it doesn't work ?
And why if I Append it just after the form tag it works magically fine ?
This is absolutely insane. I need to place the field exactly where it needs to be. An help will be appreciate. Thanks in advance.
<form id="myform" name="myform" method="post" action="somewhere.php">
// APPEND <INPUT> IN HERE WORKS !!!!
<div id="myDiv">
// APPEND <INPUT> IN HERE WONT WORK !!!!
</div>
<button type="submit" id="myform"> SAVE THE FORM </button>
</form>
It's difficult to know what's wrong, if you don't post your code.
In any case, the problem certainly isn't the div inside the form.
$('#myform').prepend('<input value="1"/>');
$('#myDiv').prepend('<input value="2"/>');
form {
background: #ff9999;
padding: 1rem;
}
div {
background: #99ff99;
padding: 1rem;
margin: 1rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="myform" name="myform" method="post" action="somewhere.php">
APPEND INPUT IN HERE WORKS !!!!
<div id="myDiv">
APPEND INPUT IN HERE WONT WORK !!!!
</div>
<button type="submit" id="myform"> SAVE THE FORM </button>
</form>
I am making a loan calculator and I want to run some calculations that are being posted from a form using the POST method. The code is not executing when I run it. Where am I am missing it?
I have run the code without the function and it seems to be working well, but the moment I put it in a function its not running.
function loancal()
{
if(isset($_POST['submit'])) {
$principal = $_POST['principal'];
$intrstRate = $_POST['intrest'];
$tenure = $_POST['tenure'];
$result = ($principal * $intrstRate * $tenure) ;
echo $result;
} else {
echo '00.00';
}
}
This is the line that is calling the function after it has been submitted:
<h1 class="title is-2"> $<?php loancal(); ?></h1>
I am expecting the out to change from $00.00 to for example a calculated result, but the output is still the same $00.00
This is the form (excerpt).
<form method="post" action="">
<input type="text" name="principal">
<input type="text" name="intrest">
<input type="date">
<input type="text" name="tenure">
<button type="submit">Calculate</button>
<button type="reset" >Clear</button>
</form>
So my very first answer is your issue, you need to give your submit button a name.
<button name="submit" class="button is-rounded is-primary" type="submit">Calculate</button>
Here's one way to get the code to execute:
<?php
function loancalc() {
$result = '0.00';
if( isset( $_POST['submit'] )){
// inspecting submitted values so that...
foreach ($_POST as $key => $value){
$bool[$key] = ctype_print( $value );
}
// ... if proven valid, then make assignments
if ( $bool['principal'] && $bool['interest'] && $bool['tenure'] ) {
// array destructuring available since PHP 7.1
[$principal, $interestRate,$tenure] =
[$_POST['principal'],
$_POST['interest'],
$_POST['tenure']];
$result = $principal * $interestRate * $tenure;
} // inner if
} // if POSTed
echo number_format( $result,2,'.',',' ); // default English representation
} // end func
?>
<html>
<head>
<title>Untitled</title>
<style>
#submit {
background: #000;
color: lime;
}
#clear {
background: #000;
color: cyan;
}
input {
background: #ffffee;
color: #303;
}
h1 {
font-size: 32pt;
margin-bottom: 3em;
color: #f0c;
}
</style>
</head>
<body>
<h1>$
<?php loancal(); ?>
</h1>
<form method="post" action="">
<input type="text" name="principal" id="principal">
<input type="text" name="intrest" id="intrest">
<input type="date">
<input type="text" name="tenure" id="principal">
<button type="submit" name="submit" id="submit">Calculate</button>
<button type="reset" id="clear">Clear</button>
</form>
</body>
</html>
The form values will only be properly submitted if they possess name attributes. Id attributes are optional and handy for front-end code manipulation.
Important point: Be sure to treat user submitted data as suspect, i.e. possibly tainted. In this example, a simple check makes sure that the the user input values only contain printable characters.
(Related demo of PHP code here.)
Refinements:
gave $result default value and eliminated an if-else clause
formatted the number with number_format() to make the number more user-friendly.
added some styling for fun :)
I have below a short program. When I press the submit button, the button is supposed to turn green. This works as intended. However, when I press the submit button, the whole page reloads and gives an alert that I only intends it to do when the page initially loads and not every time when I press the submit button. Every time I press the submit button, the only thing that I intends is to have the button turn green. Is there a way to prevent the first line of php from executing when I press the submit button? Much appreciated.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles/normalize.css">
<style type="text/css">
.sub
{
background-color: #1f5a7c;
border: none;
font-weight: bold;
cursor: pointer;
}
</style>
</head>
<body>
<form method="POST" >
<input type="submit" name="TestButton" id="TestButton" value="Test Button" class="sub">
</form>
</body>
</html>
<?php
echo "<script type='text/javascript'>alert('Start')</script>";
if(isset($_POST["TestButton"]))
{
echo "<script type='text/javascript'>TestButton.style.background='green';</script>";
}
?>
That php will run every time .. You can use an else to prevent this.. IE
<?php
if(isset($_POST["TestButton"]))
{
echo "<script type='text/javascript'>TestButton.style.background='green';</script>";
}else{
echo "<script type='text/javascript'>alert('Start')</script>";
}
?>
Try this:
<?php
if(!isset($_POST['TestButton'])) {
echo "<script type='text/javascript'>alert('Start')</script>";
}
<?php>
You can use PHP conditional statements to skip the part of code.
You can wrap states of the form in if...else statement.
<?php
if(isset($_POST["TestButton"])) {
?>
<form method="POST" >
<!-- Here you can change it as you want to -->
<input type="submit" name="TestButton" id="TestButton" value="Test Button" class="sub green someother_class">
</form>
<?php
else {
?>
<form method="POST" >
<input type="submit" name="TestButton" id="TestButton" value="Test Button" class="sub">
</form>
<?php
}
?>
Or you can make it short by adding a variable and changing it's value accordingly-
<?php
if(isset($_POST["TestButton"])) {
$classes = "sub green some_other_class";
else {
$classes = "sub";
}
?>
<form method="POST" >
<!-- Here you can change it as you want to -->
<input type="submit" name="TestButton" id="TestButton" value="Test Button" class="<?php echo $classes; ?>">
</form>
I'm trying to use an a tag to submit a form but the PHP doesn't seem to react:
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['submit']))
echo 'dog';
else
echo 'cat';
$currentPage = 'test';
echo <<<eod
<form method='post' action='$currentPage.php' id='sign_in' style='margin: 0; padding: 0;'>
Username: <input type='text' name='username' style='width: 90px;'>
Password: <input type='text' name='password' style='width: 90px; border-right: 1px solid #bbb'>
<a id='sign_in_submit' name='submit'>Sign in</a>
</form>
eod;
?>
<script>
var form = document.getElementById('sign_in');
document.getElementById("sign_in_submit").addEventListener("click", function () {
form.submit();
});
</script>
</body>
</html>
tried three methods of submission via JS but none of them seem to work
Your submit a tag would not be included in the post data, so check for the existence of one of the input tags, say $_POST['username']
try in javascript this:
document.sign_in.submit();
and make a submit button like this
<input type="submit" id='sign_in_submit' value="Sign in" name='submit'
I have just tested your code. You are checking if submit field/button is sent over the form, but you do not have anything named submit in your form. You have got username and password, so changing your if(isset($_POST['submit'])) to if(isset($_POST['username'])) or if(isset($_POST)) will fix your issue.
$jepse = $_POST['slj'];
if(isset($jepse)){
$sql43 = "UPDATE notifications SET seen='1' WHERE touser='$myid' ";
if(mysqli_query($con, $sql43)){
}}
?>
<center>
<form action="#" method="POST">
<input type="submit" name="slj" value="Seen" style="background: rgba(255,255,255, 0); ">
</form>
</center>
I have very stupid problem here... My submit button wont set ..... Don't know what is problem... I have many same things on different pages and with different names... But for this it wont work .... Anyone help?
:::::::::::::::::::UPDATE:::::::::::::::::::::::
I did it with moving update code to another file and at action set that file...
A submit button alone isn't enough to post a value. You should use a form field like a hidden input to post your data. I made this stupid mistake also.
<input type="hidden" name="slj" value="some-value">
You doesn't set any values to submit to the server.
You have to change the form like this:
<center>
<form action="" method="POST">
<input type="hidden" name="slj" value="1">
<input type="submit" value="Seen" style="background: rgba(255,255,255, 0); ">
</form>
</center>
A value in the submit button is just the text printed on the button. You have to set another hidden input value and that will be sent.
Also it could be heplful to change the action to "".
your codding is expected to work as is if there are no spelling mistakes somewhere: Although the
defining a POST element before checking if it is set, the program will see it as it is not defined ref: $jepse = $_POST['slj']; but that should not be the main problem why your database is not updating since once you clicked the button it was then defined.
Also in your form <form action="#"... remove the #
i.e. if you are on the same page <form action=""...
or
i.e. be direct <form action="the-php-page.php"...
<?php
if(isset($_POST['slj'])){
$sql43 = "UPDATE `notifications` SET `seen`='1' WHERE `touser`='$myid' ";
if(mysqli_query($con, $sql43)){
echo "updated";
}
else
{
echo "Error updating record: " . mysqli_error($con);
}
}
?>
<span>
<form action="" method="POST">
<input type="submit" name="slj" value="Seen" style="background: rgba(255,255,255, 0); ">
</form>
</span><br>
REGARDS
<center>
<form method="POST">
<input type="submit" name="slj" value="Seen" style="background: rgba(255,255,255, 0); ">
</form>
</center>
<?php
$jepse = $_POST['slj'];
if(isset($jepse)){
echo "Working fine";
}
?>
Let mw know whether this code return "Working fine" or not