This question already has answers here:
Showing error without page refresh
(3 answers)
Closed 9 years ago.
I'm trying to create a comment section for my website and need to make it so that after the person submits a comment (and it contains errors), it will show the error (a new HTML element) without refreshing the page. I know almost nothing of AJAX/JQuery, so I'll need some help.
Here's what I have so far:
<?php
if(isset($_POST['reply_submit'])) {
$reply = $_POST['new_reply'];
$reply_message = "";
if(empty($reply)) {
$reply_message = "Your comment is too short.";
}
}
?>
<html lang="en">
<body>
<form class="no-margin" method="POST" action="">
<textarea name="new_reply" placeholder="Write a reply..."></textarea>
<button name="reply_submit" class="btn post-button" type="submit">Post' . (isset($reply_message) ? '<div class="comment-warning">' . $reply_message . '</div>' : '') . '</button>
</form>
</body>
</html>
So what I need it to do is, if the person's comment box doesn't meet the criteria (in this case, an empty field), I need it to show this error line without refreshing the page:
<button name="reply_submit" class="btn post-button" type="submit">Post' . (isset($reply_message) ? '<div class="comment-warning">' . $reply_message . '</div>' : '') . '</button>
Please help.
HTML mostly the same, add onsubmit action
<form class="no-margin" method="POST" action="" onsubmit=verify();>
<textarea name="new_reply" id="new_reply_textarea" placeholder="Write a reply..."></textarea>
...
</form>
<div class=comment-warning></div>
JS
function verify()
{
if ($('#new_reply_textarea').is(':empty'))
{
$('.comment-warning').html("Your comment is too short.");
return false;
}
else
{
return true;
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this question
I am creating a small game app with a small "login" system . The user on index.php needs to input a username in order to proceed .
<form action="game.php" method="POST" class="box">
<h5 class="text-center">Provide a info</h5>
Enter username :
<input type="text" name="username" class="form-control form-control-sm text-secondary m-0">
<input type="submit" value="ENTER" class="btn btn-md btn-danger text-light mt-1">
</form>
After user has inputed his username on other page game.php I check are values entered if not die() message will appear . Piece of code for that is below .
if(isset($_POST['username'])){
// get username value from post
$username = $_POST['username'];
// checker() function checks do username already exists in DB
$found = checker();
var_dump($username);
if(!$found) {
// call a function to create a user
// and generate a unique token
$token = randomString();
createUser();
}
} else die("need to fill a input field");
Below This I have system for upgrade .
For example I have woodStock and woodLevel variables.
In game.php in HTML code below I created a from with action on PHP_SELF and method=POST .
Code below :
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" action="POST">
// submit button with name="wood" specified for a wood Upgrade
<input type="submit" value="Upgrade" class="btn btn-sm btn-light text-dark" name="wood">
</form>
In PHP script I had specified a if statement with isset function to determine is Button clicked or not .
Code :
if(isset($_POST['wood'])) {
var_dump($username);
// calls a upgradeWood() function and arguments are passed
upgradeWood($woodLevel,$woodStock);
}
The problem in all this case is that whenever I click a Upgrade button script throws a error from
die() function .
output : need to fill a input field
I had already tried everything . Even setting up a session but it is not working .
Have no idea what to do .
Please help me .
Tryed with session_start() and $SESSION superglobal . But it is still confusing to me .
I am still very new to PHP and Backend.
This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I am learning PHP and trying to test out some code but it won't work and I can't figure out why. I am trying to make a simple form where:
A)When the page first loads beneath the form it says "You have not written anything yet."
B)If something is written in the form field and submitted then beneath the form it says "Your message has been sent"
C)If nothing is written in the form field but the user hits submit that it says "Error: You didn't write anything".
I can get the first two parts to work but no matter what I do I can't get C to work.
Here is my code:
<?php
$oldname = isset($_POST['name']);
?>
<!DOCTYPE html>
<html>
<head>
<title>Form Test</title>
</head>
<body>
<form method="POST" action="">
<label for="name">Your Name:</label>
<input type="text" name="name" id="name">
<input type="submit" value="submit">
</form>
<?php
if ($oldname) {
if ($_POST['name'] = '') {
echo "Error: You didn't write anything";
}
else {
echo "Your message has been sent";
}
}
else {
echo "You have not written anything yet";
}
?>
</body>
</html>
You wrote an assignment in your if statement:
$_POST['name'] = ''
I think you meant to do a comparison:
$_POST['name'] == ''
You have a typo. You were using assignment operator in the if condition which makes it true all the time so it never enters else condition "Your message has been sent".
I have fixed the code. Take a look at it.
<?php
$oldname = isset($_POST['name']);
?>
<!DOCTYPE html>
<html>
<head>
<title>Form Test</title>
</head>
<body>
<form method="POST" action="">
<label for="name">Your Name:</label>
<input type="text" name="name" id="name">
<input type="submit" value="submit">
</form>
<?php
if ($oldname) {
if ($_POST['name'] == '') {
echo "Error: You didn't write anything";
}
else {
echo "Your message has been sent";
}
}
else {
echo "You have not written anything yet";
}
?>
</body>
</html>
I'm trying to display one of two buttons depending on "magnum"(printername) being in present in the array "booleans".
My problem is that when the form gets posted, the data retrieved on page load is correct, but the buttons displayed are incorrect. if clicked on a button, the form posts and refreshes the page, "magnum" gets pushed into $_SESSION['booleans'] but the button still displays "btn btn-default", so it requires another page refresh for the button to load correctly('btn btn-succes').
Is my problem due to $_SESSION or am i missing something?
echo'
<form class="form1" method="post" action="" id="form1">
<div class="col-xs-offset-1 col-xs-2">';
if(in_array('magnum', $_SESSION['printers'])){
if(in_array('magnumBool',$_SESSION['booleans'])){
echo '<input type="submit" name="unSubmitMagnum" id="magnumBool" value="magnum" class='.$enabled_printer.'>';
if(isset($_POST['unSubmitMagnum']) && $_POST['unSubmitMagnum']){
$pos = array_search('magnumBool', $_SESSION['booleans']);
unset($_SESSION['booleans'][$pos]);
dump('unset');
}
}
elseif(!in_array('magnumBool',$_SESSION['booleans'])){
echo '<input type="submit" name="submitMagnum" id="magnumBool" value="magnum" class='.$disabled_printer.'>';
if(isset($_POST['submitMagnum'])&& $_POST['submitMagnum']){
array_push($_SESSION['booleans'],'magnumBool');
dump('set');
}
}
}
else{
echo '<button id="magnum" class='.$lost_connection_printer.'>1. Magnum</button>';
}
echo '
</div>
</form>';
$_SESSION['printers'] is an array containing "magnum" -
$_SESSION['booleans'] is the array which isn't working as i would like it to -
$enabled_printer = "btn btn-success" <br>
$disabled_printer = "btn btn-default" <br>
$lost_connection_printer = "btn btn-danger disabled"
The problem is that you are mixing elaboration and printing. Try to split you code, so it will work and it will be more readable:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['unSubmitMagnum']) && $_POST['unSubmitMagnum']) {
$pos = array_search('magnumBool', $_SESSION['booleans']);
unset($_SESSION['booleans']['magnumBool']);
} elseif (isset($_POST['submitMagnum'])&& $_POST['submitMagnum']) {
$_SESSION['booleans']['magnumBool'] = true;
}
}
echo'<form class="form1" method="post" action="" id="form1">
<div class="col-xs-offset-1 col-xs-2">';
if(in_array('magnum', $_SESSION['printers'])){
if(isset($_SESSION['booleans']['magnumBool'])){
echo '<input type="submit" name="unSubmitMagnum" id="magnumBool" value="magnum" class='.$enabled_printer.'>';
} else {
echo '<input type="submit" name="submitMagnum" id="magnumBool" value="magnum" class='.$disabled_printer.'>';
}
}
else{
echo '<button id="magnum" class='.$lost_connection_printer.'>1. Magnum</button>';
}
echo '</div>
</form>';
P.s. note the use of "magnumBool" as a key isset instead of as a value: in this way (when possible) you will avoid duplicate entry and will make you code lighter if you have large arrays ;)
P.p.s. try always to keep you login separate from you template, this will make your code more readable and easier to maintain
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
with this example I recap my problem:
The first time I execute this php page I want to echo "I have two buttons".When the user click buttons "next" or "next1" I want to echo "I already said I have two buttons" and not "I have to buttons" again.I proved also with hidden input text but it doesn't work. thk for help
<?php
if ($_SESSION['already_said'] == false ){
echo "I have two buttons". date('h:i:s')."<br>";
}
$_SESSION['already_said']=true;
if( isset($_GET["next"]) || isset($_GET["next1"])) {
echo "I already said I have two buttons". date('h:i:s')."<br>";
}
?>
<html>
<body>
<form name="form" method="GET" action="" >
<button type="submit" name="next">
<span class="label">Next</span>
</button>
<button type="submit" name="next1">
<span class="label">Next1</span>
</button>
</form>
</body>
</html>
Of course the important thing is to remember to put the session_start() at the top of any script that uses the session. In fact it is a good idea to put it in all your scripts even if a few dont use the session just to keep the session alive.
<?php
session_start();
$msg = "I have two buttons ". date('h:i:s')."<br>";
if (! isset($_SESSION['already_said']) ){
$_SESSION['already_said'] = true;
}
if( (isset($_GET["next"]) || isset($_GET["next1"]) ) && isset($_SESSION['already_said'])) {
$msg = "I already said " . $msg;
}
?>
<html>
<body>
<div> <?php echo $msg; ?></div>
<form name="form" method="GET" action="" >
<button type="submit" name="next">
<span class="label">Next</span>
</button>
<button type="submit" name="next1">
<span class="label">Next1</span>
</button>
</form>
</body>
</html>
*<form action="" method="post">
<br><br><br>
<label style="padding-left:40px; font-size:28px; height:24px;">To</label>
<input type="text" name="mail" style="width:280px; height:24px;"></input>
<br><br>
<textarea rows="10" cols="41" name="notify" style="margin-left:50px;">
</textarea>
<br><br>
<input type="reset" value="Reset" style="margin-left:80px; width:100px; height:30px;"></input>
<input type="submit" value="Send" name="send" style="margin-left:50px; width:100px; height:30px;"></input>
</form>*
this is a part of my html code
*<?php
if(isset($_POST['mail'])&&isset($_POST['notify']))
{
$email=$_POST['mail'];
$notification=$_POST['notify'];
if(!empty($email)&&!empty($notification))
{
include "db.php";
$query="insert into notification(email,notification) values('$email','$notification')";
$result = mysql_query($query);
if($result)
{
$_SESSION['mail']=$email;
$_SESSION['notify']=$notification;
}
}
}
?>*
this is a part of my php code. I have started the session already
The code is running fine I can enter email and notification in database. But I want the notification to get displayed in another page. I can display the notification. But each time I submit notification to the same email the new notification replaces the old notification. I want to show every notification each time user submits in a new page.
I have coded for new page as follows
*<?php
session_start();
if($_SESSION['login_user']==$_SESSION['mail'])
{
echo '<div id="notificationdiv" style="background-color:#CFC; width:100%; height:30px">'.$_SESSION['notify'].'</div>';
}
?>*
Hopefully I am understanding you correctly.
But from the looks of it you will need to append / add the new notification to the old one by concatenation:
For example (example 1):
$_SESSION['notify'] .= $notification . ',';
Currently you are over writing your previous notification.
In the above example I have split the notifications with a comma.
you can use and other special character to separate them for example.
For example (example 2)
$_SESSION['notify'] .= $notification . '/';
You can then use explode to separate the notifications
For Example (for example 1):
$notifications = explode( ',' , $_SESSION['notify']);
foreach($notifications as $notification){
echo '<div>' . $notification . '</div>';
}
For Example (for example 2):
$notifications = explode( '/' , $_SESSION['notify']);
foreach($notifications as $notification){
echo '<div>' . $notification . '</div>';
}