I have two buttons, one to log in and another to log out.
Now I want only one button, I want that it only shows the log in button.
And if you are logged in, then the page refresh (already done) but then I want that the log in button is changed into a logoff button. So that you don't log in two or more times a day and you have to log out first.
<form action="begintijd.php" method="POST">
<input name="begintijd" value="aanmelden" type="submit">
</form>
<form action="eindtijd.php" method="POST">
<input name="eindtijd" value="afmelden" type="submit">
</form>
header("Refresh: 2; index.php");
So you can log in and off so many times you want. But I only want that you can log in once and then you first have to log out before you can log in.
(some things are in dutch, you can overwrite it if you want)
Simple.
You are using a cookie to set login data. If cookie is set, then display button 1, else display button 2, like;
<?php
if (isset($_COOKIE["user"])){
echo '<form action="begintijd.php" method="POST">
<input name="begintijd" value="aanmelden" type="submit">
</form>';
}else{
echo '<form action="eindtijd.php" method="POST">
<input name="eindtijd" value="afmelden" type="submit">
</form>';
}
?>
Use sessions: Using sessions & session variables in a PHP Login Script
<?PHP
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
echo '<form action="begintijd.php" method="POST">'.
'<input name="begintijd" value="log in" type="submit">'.
'</form>';
}
else
{
echo '<form action="begintijd.php" method="POST">'.
'<input name="begintijd" value="log out" type="submit">'.
'</form>';
}
?>
You need to read about session variables or cookies.
If your are using sessions.You can do using this.
<?php if($_SESSION['logged_in']==true){?> //if user is logged in show logout button
<form action="begintijd.php" method="POST">
<input name="begintijd" value="aanmelden" type="submit">
</form><?php } else {?>//if user is logged out show login button
<form action="eindtijd.php" method="POST">
<input name="eindtijd" value="afmelden" type="submit">
</form><?php }?>
You can try this.
<form action="<?=($_SESSION['login']!="")?"begintijd.php":"eindtijd.php"?>" method="POST">
<input name="<?=($_SESSION['login']!="")?"begintijd":"eindtijd"?>" value="<?=($_SESSION['login']!="")?"aanmelden":"afmelden"?>" type="submit">
</form>
Send us your begintijd.php and eindtijd.php codes
Related
I've got a php page that gets data from mysql database as $certs[] and populates that as a table:
($db is read from file, it is constant)
<?php echo $cert["id"] ?>
<button type="button" class="awe" title="Edit" id="editbtn" onclick="openNewModalWithValue('Modal2')" value =<?php echo $cert["id"] ?> ></button>
<form action="sample.php" method="POST">
<input type="hidden" name="id" value=<?php echo $cert["id"] ?>>
<input type="hidden" name="db" value=<?php echo $db ?>>
<button type="submit" name="mission" title="Delete"></button>
</form>
And I try to get id value in Modal window. (Like press 'EDIT' button and you get modal window with values set.) So my Modal window is:
<form id="modal-form" method="POST" action="sample2.php">
<input type="number" id="idb" name="idbase" />
<input type="text" id="nme" name="name"/>
<input type="hidden" name="db" value=<?php echo $db; ?>>
<input type="hidden" id= "idbs" name="idbs" />
<button id="form-submit" type="submit" >Edit</button>
</form>
and the Javascript for opening modal window is:
function openNewModalWithValue(modal){
document.getElementById(modal).style.display = "block";
document.getElementById("idb").value = document.getElementById("editbtn").value;
}
The problem is that even though PHP sends $certs[] values correctly, and table populates correctly, when I hit 'edit' button, I get preserved values of item#1 in PHP array, no matter which row/data I click.
Is there an easier way rather than applying counter to every row and operating with it?
Problem may be idb is multiple id in DOM. document.getElementById find Dom in whole page. so you must provide unique id
I want to create a button that opens a new tab with the user's name. But in Chrome it always block the popup window. Is there way to do this without enabling popup windows?
<div class="box">
<form action="" method="post">
<input type="text" name="meno" class="form-control" placeholder="Nick">
<input type="submit" name="submit" class="tlacitko" value="Submit">
</form>
<?php
if($_POST){
$nick = $_POST['meno'];
$hlasovat = "<script>window.open('https://czech-craft.eu/vote?id=16942&user=$nick') </script>";
echo $hlasovat;
unset($_POST);
}
?>
</div>
If you want your HTML form to open in a new window/tab when submitted you can use:
<form action="" method="post" target="_blank">
instead of
<form action="" method="post">
For your code to work using this technique you will probably also need to do something like:
$hlasovat = "<script>window.location.href = 'https://czech-craft.eu/vote?id=16942&user=$nick' </script>";
or
$hlasovat = "<script>window.location.assign('https://czech-craft.eu/vote?id=16942&user=$nick') </script>";
or
$hlasovat = "<script>window.location.replace('https://czech-craft.eu/vote?id=16942&user=$nick') </script>";
instead of
$hlasovat = "<script>window.open('https://czech-craft.eu/vote?id=16942&user=$nick') </script>";
The client side validation looks something like this:
function validateFname() {
var x = document.getElementById('fname').value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}
The server side below it:
if(isset($_POST['SubmitForm'])){
if(empty($_POST['fname'])){
echo "First name cannot be empty!<br/><br/>";
return false;
}
and the form itself below that:
<form name = 'checkout' method='post' action="<?php echo $_SERVER['PHP_SELF']; ?>"
accept-charset='UTF-8'>
First Name:<input type="text" name="fname" id="fname" onsubmit="return validateFname()"
class="required" value="<?php echo $fname;?>" />
<input type="submit" name="SubmitForm" value="Send"/>
Ofcourse there is a lot more to it but ive just given one example above. I am trying to figure out why the (clientside)javascript function is not working and instead the (server) php error is coming up. I need to be able to have the client run validation checks before the server to reduce load.
The onsubmit is a event that belongs to the <form> element. You have placed it with the submit button which won't work.
Here's a working fiddle:
Demo
Reference
<form name = 'checkout' method='post' action="<?php echo $_SERVER['PHP_SELF']; ?>" accept-charset='UTF-8'>
First Name:<input type="text" name="fname" id="fname" value="<?php echo $fname;?>" />
<input type="submit" name="SubmitForm" onClick="return validateFname()" value="Send"/>
</form>
try the above code. Instead of using onSubmit in input text field use onClick in submit button.
good luck
I have a php page. In the bottom there is a contact form. What I want to do is to hide the contact form when the mail is sent and instead show a thank you part. Means that when customer comes first time to the page the form show up. After submit the thank you part show up.
I have seen it done but have no clue how.
I have an idea that when the page loads it must check a variable to see if the mail was sent, but perhaps this is wrong.
When I need stuff like this, I usually name the submit button "submit" and checks if it is set...
<input type="submit" name="submit" value="Send form">
And the php (Use $_GET or $_POST accordingly)...
<?
if(isset($_POST['submit']))
{
// Show "Thank you"
}
else
{
// Show form
}
?>
When I do things like this I tend to add an button to my form and give it a name
<button type="submit" id="submit" name="submit">Submit</button>
Then in my PHP check if the form has been submitted
<?php
if(isset($_POST['submit'])) {
// Actions After Submit
}
else
{
// Load The Form
}
?>
It's rather quite simple; add exit("Message..."); after mail()
In your PHP where the mail(...) is located, you would do the following:
mail($to,$subject,$message,$headers);
exit("Thank you, your message has been sent. <a href='home.php'>Click here</a> to return to our Website.");
Here is a complete basic solution using pure PHP, since no code has been provided.
The following is meant to be run as everything inside the same file.
<?php
if(isset($_POST['send'])) {
// Prepare the email
$to = 'email#example.com';
$mail_from = $_POST['email'];
$subject = 'Message sent from website';
$message = $_POST['message'];
$name = $_POST['name'];
$header = "From: $name <$mail_from>";
// Send it
$sent = mail($to, $subject, $message, $header);
if($sent) {
exit("Thank you, your message has been sent. <a href='home.php'>Click here</a> to return to our Website.");
} else {
echo 'Sorry, your message could not be sent. The error has been logged.';
}
} // closing brace for if(isset($_POST['send']))
?>
<form method="post" action="">
<p>
<label for="name">Name</label>
<input type="text" id="name" name="name" />
</p>
<p>
<label for="email">Email</label>
<input type="text" id="email" name="email" />
</p>
<p>
<label for="message">Message</label>
<textarea id="message" name="message" rows="6" cols="30"></textarea>
</p>
<p>
<input type="submit" name="send" value="Send message" />
</p>
</form>
It's very simple, just if and else,sample form is below
<?php
if(isset($_POST['submit'])) { ?>
//write your other actions on form data
<h2>THANK YOU</h2>
<?php
} else { ?>
<form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label><h4>Username:</label>
<input type="text" name="username">
<input type="submit" name="submit" value="submit"/>
</form>
<?php }
?>
So I have pop-up window where user need to enter name, after clicking submit button this window close.
You can test it here: http://eurokos.lt/under20/button.php (Click 21 button, then try to enter any value and see what happens)
For this pop-up box I've used function:
function popUp() {
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';
}
This function is used for X button to close window:
function closeWindow() {
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none';
}
My button which open pop-up window looks like:
Echo "<input type='button' onclick='popUp();' value='".$i."' />";
And here is PHP_SELF and form:
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "</br>You have entered: <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
`
What I need to do that posted result what entered and window not closed?
Maybe I need to use popUp() function again when submitting? But how to do that correctly? Thank you.
Try this:
html:
<form method="post" action="" onsubmit="return display()">
<div id="answer"></div>
<input type="text" name="name" id="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
javascript:
function display(){
var ans = document.getElementById("name").value
document.getElementById("answer").innerHTML="You have entered:"+ans;
return false;
}
Update:
function popUp(){
document.getElementById("answer").innerHTML=""; //Add these in your popup function
document.getElementById("name").value="";
}
First of all i suggest you to avoid PHP_SELF in the action it can be manipulated by people and it's not good.
at top of page try to do :
<?php
$thispage = $_SERVER['PHP_SELF'];
?>
<html>
.
.
.
<form method="post" action="<?=$thispage?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
</html>
Then, using the action for sending data to PHP, will reload the page so you are probabilly losing all the inline fix you did.
Try to get the input values after the submit, assign them to varibles and do, for example:
xmlhttp.open("POST","<?=$thispage?>",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("name=name");
with an asynchronous call you don't reload the page and you don't lost your changes