Here is the code. I want it this way --
Form submission --> page2.php --> redirect --> page1.php (Here is the message. Pop-up or whatever)
page1.php
<form action="page2.php" method="post" enctype="multipart/form-data" class="form-inline subscribe-form">
<input type="name" name="name" placeholder="Jack">
</div>
<button type="submit" name="sub" value="sub" >Submit</button>
</form>
page2.php
<?php
//include necessary
if(isset($_POST['sub'])) {
$nameget = mysqli_real_escape_string($dbconnect, $_POST['name']);
$sqlentry = .....bla bla......//insert into DB
}
$getsql = mysqli_query($dbconnect, $);
if($getsql){
mysql_close($dbconnect);
header('location:page1.php');
}
?>
Where you have:
header('location:page1.php');
append a variable on the location, like:
header('location:page1.php?status=success');
And on page1.php, do something like:
if( $_GET['status'] == 'success'):
echo 'feedback message goes here';
endif;
This way your flash message will not show up again and again after refresh.
<?php session_start();
if(isset($_SESSION['msg']) && $_SESSION['msg'] != ''){
echo $_SESSION['msg'];
unset($_SESSION['msg']);
}
?>
<form action="page2.php" method="post"
enctype="multipart/form-data" class="form-inline subscribe-form">
<input type="name" name="name" placeholder="Jack">
</div>
<button type="submit" name="sub" value="sub" >Submit</button>
</form>
And
<?php
session_start();
//include necessary
if(isset($_POST['sub'])) {
$nameget = mysqli_real_escape_string($dbconnect, $_POST['name']);
$sqlentry = .....bla bla......//insert into DB
}
$getsql = mysqli_query($dbconnect, $);
if($getsql){
mysql_close($dbconnect);
$_SESSIOM['msg'] = 'Value Inserted or whatever';
header('location:page1.php');
}
?>
Related
i have a little problem
I have a form in login.php:
`
<form action="loginscript.php" method="post">
<h2>Login form</h2>
<label for="nick">Podaj imie: </label>
<input type="text" name="nick" id="nick">
<br>
<label for="pass">Podaj haslo: </label>
<input type="password" name="pass" id="pass">
<br>
<p>LOG IN</p>
<input type="submit" name="submit" id="submit">
</form>
`
and i have a loginscript.php file:
`
<?php
session_start();
if (isset($_POST["nick"]) && isset($_POST["pass"])) {
$nick=$_POST["nick"];
$pass=sha1(sha1($_POST["pass"]));
$conn = mysqli_connect("localhost", "root", "", "baza2");
if ($conn) {
$query = mysqli_query($conn, "SELECT * FROM login_table WHERE nick='$nick' AND pass='$pass'");
if (mysqli_num_rows($query)) {
$_SESSION["logged"]=true;
header("Location: main.php");
} else {
header('Location: login.php');
}
mysqli_close($conn);
}
}
?>
`
In the loginscript.php in else i have redirect to login.php page. How can i change maybe p tag from 'LOG IN' to 'USERNAME OR PASSWORD IS WRONG'?
I tried using jquery but that doesn't work, maybe I don't know how. Please help :(
You can't change anything on the target page from there, but what you can do is provide some information to the target page which that page can use. For example, consider this redirect:
header('Location: login.php?failed=true');
Then in the login.php code you can check for the "failed" query string value and conditionally change the output based on that. For example:
<?php
$message = isset($_GET['failed']) ? "USERNAME OR PASSWORD IS WRONG" : "LOG IN";
?>
<form action="loginscript.php" method="post">
<h2>Login form</h2>
<label for="nick">Podaj imie: </label>
<input type="text" name="nick" id="nick">
<br>
<label for="pass">Podaj haslo: </label>
<input type="password" name="pass" id="pass">
<br>
<p><?= $message ?></p>
<input type="submit" name="submit" id="submit">
</form>
you could try the code below.
if (mysqli_num_rows($query)) {
$_SESSION["logged"]=true;
echo "<script type='text/javascript'> document.location = 'main.php';</script>";
} else {
echo "<script>alert('Your Password or username is wrong!');</script>";
}
When I press the Login button it refreshes the page and doesn't do anything. I tried entering wrong Server IP just to check the error, but I guess it's not responding to the login button and directly refreshing the page.
Unable to call a PHP function in the HTML file, tried using the same code in a different code and it's working fine there but not here:
<title> Database Login </title>
<body>
</head>
<body>
<span>
<div class="heading">
<h3><img src="http://zicom.com/img/ilayashop-1482233381.jpg" alt="Zicom Logo" >
<h1><b>MAaaS Login<b></h1><br>
</span>
</div>
<div class='admin'>
<form method='post' action=''>
<p class='main'> Enter your Details </p>
<p> Enter User Name <input type="text" name="name" id="userInput"></p>
<p> Enter password <input type="password" name="pass" id="userInput"> </p>
<br>
<input id="login" type="submit" name="submit" value="Login">
</form>
</div>
<?php
if($_POST){
$UN = $_POST['name'];
$PS = $_POST['pass'];
$scon=odbc_connect("Driver={SQL Server};Server=XXXXXXX; Database=Sampledata;","XX","XXXXX");
$query="SELECT [password] FROM [Simcarddata].[dbo].[MasterUser] Where username='$UN'";
$rs=odbc_exec($scon,$query);
if (!$rs)
{
$msg="SQL statement failed with error:\n";
$msg.=odbc_error($scon).": ".odbc_errormsg($scon)."\n";
} else {
$number_of_rows = odbc_num_rows($rs);
$msg="$number_of_rows records found.\n";
}
while(odbc_fetch_row($rs))
{
$field1 = odbc_result($rs,1);
// print ("$PS and $field1");
if ($PS == $field1){
print ("TRUE");
header("Location: /test.php");
}
else {
print ("Incorrect Username or password");
}
}
odbc_close($scon);
header("Refresh:2");
}
?>
</body>
</html>
I just tidied up the HTML tags a little and used a better method of checking for POST data - but I think the issue was the badly formed HTML was confusing the form
<html>
</head>
<title> Database Login </title>
</head>
<body>
<span>
<div class="heading">
<h3><img src="http://zicom.com/img/ilayashop-1482233381.jpg" alt="Zicom Logo" ></h3>
<h1><b>MAaaS Login<b></h1>
<br>
</div>
</span>
<div class='admin'>
<form method='post'>
<p class='main'> Enter your Details </p>
<p>Enter User Name <input type="text" name="name" id="userInput"></p>
<p>Enter password <input type="password" name="pass" id="userInput"></p>
<br>
<input id="login" type="submit" name="submit" value="Login">
</form>
</div>
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['name'], $_POST['pass'] ) ){
$UN = $_POST['name'];
$PS = $_POST['pass'];
$scon=odbc_connect("Driver={SQL Server};Server=XXXXXXX;Database=Sampledata;","XX","XXXXX");
$query="SELECT [password] FROM [Simcarddata].[dbo].[MasterUser] Where username='$UN'";
$rs=odbc_exec( $scon, $query );
if (!$rs) {
$msg="SQL statement failed with error:\n";
$msg.=odbc_error($scon).": ".odbc_errormsg($scon)."\n";
} else {
$number_of_rows = odbc_num_rows($rs);
$msg="$number_of_rows records found.\n";
}
while( odbc_fetch_row( $rs ) ) {
$field1 = odbc_result($rs,1);
if ($PS == $field1){
print ("TRUE");
header("Location: /test.php");
} else {
print ("Incorrect Username or password");
}
}
odbc_close($scon);
header("Refresh:2");
}
?>
</body>
</html>
if ($_SERVER['REQUEST_METHOD'] === 'POST') use this please instead of if($_POST) or use if(isset($_POST))
<?php
session_start();
?>
<form action="HardDisk.php" method="post">//form
<input type="text" name="user" />
<?php
if(isset($_POST['user']))
{
$_SESSION['userGet']=$_POST['user'];
}
?>
<input type="submit" value="submit" />
</form>
2nd page
<?php
session_start();
?>
<?php
if (isset($_SESSION['userGet'])) {
$form_value = $_SESSION['userGet'];
}
echo $form_value ;
?>
..........................................................................................................
You have used the text box name with that check the submit button name too.
<form action="HardDisk.php" method="post">//form
<input type="text" name="user" />
<input type="submit" value="submit" name="submit" />
</form>
<?php
session_start();
if(isset($_POST['submit'])&&isset($_POST['user'])) {
$_SESSION['userGet']=$_POST['user'];
}
?>
And in your second page
<?php
session_start();
if (isset($_SESSION['userGet'])) {
echo $form_value = $_SESSION['userGet'];
}
Give the submit button a name of submit then use the following PHP code:
if(isset($_POST['submit'])){
if(isset($_POST['user']))
{
$_SESSION['userGet']=$_POST['user'];
}
}
i want to do somthing like:
1.php:
<html>
<form action=1.php method=POST enctype="multipart/form-data">
Choose a user name:<input type="text" name="username">
<input type="submit" value="Save and Proceed">
</html>
<?php
if(isset($_POST['username']))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="1.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="submit" value="done">
</form>
</html>
<?php
f( isset($_POST['age']))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "your age is ". $age;
echo"you are eligible";
}
}
}
}
?>
After the second form is submitted i do not want the script 1.php to run from the start but i want it to run the code following the form which is echoing the age only.
i do not want to go putting the later code in second script and accessing the variable of first script through making them session variables.please help. thankyou in advance
Change your condition to this
if ((isset($_POST['username'])) && ($_POST['submit'] == 'Save and Proceed')) {
}
this is the code i changed:
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
Choose a user name:</font> <input type="text" name="username">
<input type="submit" value="Save and Proceed">
</form>
</html>
<?php
if(isset($_POST['username']) && ($_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="submit" value="done">
</form>
</html>
<?php
if( isset($_POST['age']) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "your name is" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
}
}
?>
I am creating a simple form that a user submits and email. I am trying to pose an error if the form is blank or it's not a valid email and if successful, then reload the page with a success message.
When I submit blank it reloads the page without an error, and if I enter anything in ( valid or invalid email ) it reloads the page white, despite the form action being correct. I've been stuck on this and need help. Thanks.
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/system/init.php');
if(isset($_POST['submit'])) {
$email = $_POST['email'];
if(empty($_POST['email']) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "Please enter a valid email";
}else{
$success = true;
mysql_query("INSERT INTO survey
(email) VALUES('".$_POST['email']."' ) ")
or die(mysql_error());
}
}
?>
<div class="email-survey">
<?php if(isset($success)) { ?>
<div class="success">Thank You!</div>
<?php } ?>
<?php if(isset($error)) { ?>
<div class="error">
<?php echo $error; ?>
</div>
<?php } ?>
<form name="settings" action="/survey-confirm.php" method="post">
<input type="text" name="email" /> <br />
<input type="submit" name="submit" value="submit" />
</form>
</div>
<?php
function control($type, $text)
{
echo '<div class="'.$type.'">'.$text.'</div>';
}
require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/system/init.php');
if(isset($_POST['submit'])) {
$email = $_POST['email'];
if(empty($_POST['email']) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
control('error', 'Type valid mail!');
}else{
control('success', 'All done!');
mysql_query("INSERT INTO survey
(email) VALUES('".$_POST['email']."' ) ")
or die(mysql_error());
}
}
else
{echo 'echo '<form name="settings" action="/survey-confirm.php" method="post">
<input type="text" name="email" /> <br />
<input type="submit" name="submit" value="submit" />
</form>
</div>';}
?>
This is small function named control, you can call this and put your custom div name and text to show user.
control('THIS IS DIV NAME','THIS IS MESSAGE FOR USER')