document object confusion - php

In file1.php, I execute file2.php with
<form action="file2.php" method="POST">
Within file2, I want to access html elements from file1, but their document objects are different.
How can I access file1's elements from within file2?

well rather then using javascript you can access what you need via PHP's $_POST function so if you had the elements name and password you could access them with:
$_POST['name'];
$_POST['password'];
so something like this for file2.php:
<?php
if(array_key_exists('submit', $_POST))
{
$username = $_POST['name'];
$password = $_POST['password'];
echo("Hello $username, your password is $password");
}
?>
That goes on the assumption of file1.php looking like:
<form action="file2.php" method="post">
Username:
<input type="text" name="name" />
<br />
Password:
<input type="password" name="password" />
<br />
<input type="submit" name="submit" />
</form>

Why don't you try hiding in file2.php the elements that you still need?.
Just render them again with php in file2.php, and hide them via CSS, then they will be easily accesible through javascript.

Related

mysql_query("INSERT INTO... is not working

I'm trying to create a registration page. The page is successfully connected to phpMyAdmin database but it does not echo anything when i click the register button.
<html>
<head>
</head>
<body>
<?php
INCLUDE "connect.php";
INCLUDE "functions.php";
INCLUDE "titlebar.php";
?>
<div id="loginform">
<h1>Register</h1>
<form name="Register" action="register.php" method="post">
<?php
if(isset($POST["submit"])){
$username = $_POST["username"];
$password = md5 ($_POST["password"]);
if(empty($username) or empty($password)){
echo "<p>Fields Empty!</p>";
} else {
mysql_query("INSERT INTO login VALUES('',$username','$password','2','')");
echo "<p>Successfully Registered!</p>";
}
}
?>
<p>
<label for="username">Username: </label>
<input type="text" name="username" id="username"/></p><p>
<label for="password">Password: </label>
<input type="password" name="password" id="password"/></p><p>
<input type="submit" name="submit" value="Register" />
</p>
</form>
</div>
</body>
The problem is with the post method.
use $_POST instead of $POST
You have mysql error
Not: $username'
but '$username'
And next time display mysql errors with mysql_error().
At the beginning, I am not sure what isset($_POST['submit'] should return, but as already mentioned in the comments you missed a single quote.
Additionaly i would use:
$password = password_hash($_POST['password'],
md5 is deprecated and thus not safe. If you write a login script you can use password_verify(plainPW, hashPW)
You also need to specify a database and login into it. I recommend to look at the W3 Schools examples they are very in-depth and have good examples.
W3 school mysqli page
also write a die() at the end of your script and do not foregt to close the connection.

how to call string from HTML to 2 seperate PHPs?

I have log-in page, now I want to use the username input for two separate PHPs. how can I do it?
<form method = "post" action="login.php">
<input type = "hidden" name = "submitted" value = "true"/>
<h1>НЭВТРЭХ</h1>
<div>
<input type="text" placeholder="Student ID" required="" id="username" name="username"/>
</div>
<div>
<input type="password" placeholder="Password" required="" id="password" name="password"/>
</div>
<div>
<input id="button" type="submit" name="submit" value="НЭВТРЭХ" />
Lost your password?
</div>
</form><!-- form -->
The login.php is the first php calling the string. this php gets the string and works fine. but i want to use this string for another PHP.
In your login.php file you do something like this:
<?php
// Start the session engine for this php file
session_start();
// Might want to add more validation to those variables before using them
if(isset($_POST['username']) && isset($_POST['password']))
{
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
}
?>
and then you can access those $_SESSION variables from the other php file.
See W3School for more info on sessions.
In login.php store all data to session and use
header('Location: http://www.example.com/');
in second script unset those values of session

XAMPP : My input (name and pass) always has root and password inside of it..how so?

Today I was trying to create a login page with PDO class in PHP. It worked fine but my problem is my input always have a "root" string value for my first input, and a password inside my second input. Why and how can I erase that? I tried to put a "placeholder" inside of each input, and it still doesn't replace it. Oh, by the way, my input has a "yellow" background...kinda weird. And when I erase it manually, they return white as usual..
This is my code :
<?php
include_once('user.php');
if (isset($_POST['submit'])) {
$name = $_POST['username'];
$pass = $_POST['password'];
$object = new User();
$object->Login($name,$pass);
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?= $_SERVER['PHP_SELF']; ?>">
Username: <input type="text" name="username"><br><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" name="submit" value="Log In">
</form>
</body>
</html>
Cache, or saved credentials problem.
Try:
<input type="text" name="username" value="" placeholder="" autocomplete="off">

Session variables from form not showing on next page

I can set session variables and use them on another page. However when i try to use a simple contactform with a username and email address and try to store them into session variables, they don't show up on other pages. There must be something basic i'm missing.
Here's the form:
<?php
session_start();
$submit = $_POST["submit"];
if($submit){setSessionVars();}
function setSessionVars() {
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
header('Location: session.php');
}
?>
<html>
<body>
<form action="session.php" method"post">
<input name="name" type="text" value="Name" size="11" /><br />
<input name="email" type="text" value="Email" size="11" /><br /><br />
<input name="submit" type="submit" value="Submit" size="11" />
</form>
</body>
</html>
And this is session.php:
<?php
session_start();
echo $_SESSION['name'];
echo $_POST['name'];
?>
Also
header('Location: session.php');
is not working. Any ideas?
At a glance, I see one immediate problem that will keep the form from posting.
<form action="session.php" method"post">
You need an "=" sign between method and "post".
Changing that alone will give you the "t" in session.php.
You post the form to session.php:
<form action="session.php" method"post">
I'd change it to:
<form method="post">
That way, the page posts to itself. Then it can register the session variables and redirect the user to session.php.
Edit: also, you forgot the = sign in method"post".

PHP $_POST not working

I'm following a PHP tutorial which is teaching about $_POST and it had me create an exercise with two pages. On the first page (Form page) it creates a form where a user enters Username and Password. Once they click submit, it opens on the second page (process.php) where the Username and Password should be displayed as a result of using $_Post. However, when I click submit on the first page, it takes me to the second page where only the ":" in the Echo statement is displayed. No username, no password.
Any ideas? I've copied the form page and the process.php below.
Form page
<html>
<head>
<title>encode</title>
</head>
<body>
<form action="process.php" method="post">
Username: <input type="text" name="username" value="" />
<br/>
Password: <input type="password" name="password" value=""/>
<br/>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
Process.php
<html>
<head>
<title>encode</title>
</head>
<body>
<?php
$username = $_Post['username'];
$password = $_Post['password'];
echo "{$username}: {$password}";
?>
</body>
</html>
Try using $_POST (all caps).
Functions are case insensitive, but variables aren't.
$_Post['username'] doesn't work. Must be $_POST['username']. Same with the password field. You need to enable error reporting in your php.ini configuration file. It would have told you the problem.
Variable names in PHP are case sensitive. You wrote $_Post where it should be $_POST
As far as I can tell, PHP is case-sensitive when it comes to variable names, so change:
$username = $_Post['username'];
$password = $_Post['password'];
To:
$username = $_POST['username'];
$password = $_POST['password'];

Categories