I have this PHP which picks up information from a form in HTML and I wanted to put that information on a div on other HTML
Nome: <?php
global $user;
$user = $_POST['user'];
echo ucfirst($user) ?>
<br />
Password: <?php
global $pass;
$pass = $_POST['pass'];
echo $pass ?>
<br />
Data de nascimento: <?php
global $date;
$date = $_POST['date'];
echo $date ?>
I have this HTML
// HTML that I get the information from
<form method="post" id="formDetails">
<label for="name">Nome: </label> <br>
<input type="text" id="username" name="user" placeholder="username"> <br> <br>
<label for="password">Password: </label> <br>
<input type="password" id="password" name="pass" placeholder="password"> <br> <br>
<label for="date">Nascimento: </label> <br>
<input type="text" name="date" id="datepicker" placeholder="Data de Nascimento"> <br> <br>
<button type="submit" id="btn">Submit </button>
</form>
// HTML that I want to insert the data
<h1> Dados Pessoais </h1>
<div>
<div class="mensagem" id="mensagem"></div>
</div>
And I'm using this js line
// AJAX PART
var uName = $("input[name=user]").val();
var passwrd = $("input[name=pass]").val();
var dat = $("input[name=date]").val();
$("#formDetails").submit(function(){
$.post('process.php', {user: uName, pass: passwrd, date: dat})
.done(function(data) {
window.location.href = 'informacoes.html';
$('.mensagem').html(data);
$('.mensagem').show();
});
});
I had this code before and it didn't work either
// AJAX PART
var uName = $("input[name=user]").val();
var passwrd = $("input[name=pass]").val();
var dat = $("input[name=date").val();
$("#formDetails").submit(function(){
$.ajax({
type: 'POST',
url: 'process.php',
data: $("form").serialize(),
success : function(data) {
if($("#mensagem").hasClass('mensagem')){
$(".mensagem").html(data);
$(".mensagem").show();
} else {
$(".back").html(data);
$(".back").show();
}
},
error: function (xhr,ajaxOptions,throwError){
alert("erro");
},
});
return false;
I tried to get the information from data and put it inside the div class "mensagem" and show her but when it submits goes to the other HTML neither shows me the div neither the information and when I inspected element on browser I put the div visible and the information wasn't there and I don't really know what to do and I searched and I couldn't find anything useful
You are just making it to complicated for a simple form.I would suggest you to make a connection.php file where in you connect you database to Your table
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "podcast";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
create an index.php file.
Create a regular html submit form.
Include this button to your form.
<button type="submit" id="btn" name="submit-btn">Submit </button>
Add this piece of php code above and this creates a function to your button
<?php
require $connection;
if(isset($_POST['submit-btn'])){
$user = $_POST['user'];
$pass = $_POST['pass'];
$date = $_POST['date'];
try{
$insert_query = "INSERT INTO `table_name`(`column_1`, `column_2`, `column_3`) VALUES (:column_1, :column_2, :column_3)";
$query_exec = $con->prepare($insert_query);
$query_exec ->execute(array(":column_1" => $user, ":column_2" => $pass, ":column_3" => $date));
}
catch(PDOException $e) {
echo "Error: Data Insertion Failed";
}
}
?>
Related
I am trying to create website with login form with some PHP code, were user will try to login with username and password and page will then show "welcome....". AT the moment when user try to put username and password website that shows up is blank, nothing is on it. Also i already have created mysql database with username and password.
this login form on my main page index.html:
<form id="form" method="post" action="login.php">
<label for="username">Username:</label>
<input type="text" name="username" size="15" required="required" />
<label for="password">Password:</label>
<input type="password" name="password" size="15" required="required" />
<input id="loginButton" type="submit" name="submit" value="LOGIN" />
</form>
and this is my php page login.php:
<?php
session_start();
$host = "localhost";
$username = "*******";
$password = "*******";
$db_name = "********";
$tbl_name = "users";
$conn = mysql_connect("$host", "$username", "$password") or die("Cannot connect");
mysql_select_db("$db_name", $conn) or die("Cannot connect");
$myusername = $_POST['username'];
$mypassword = $_POST['password'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if ($count == 1) {
session_register("username");
session_register("password");
header("location:page1.html");
} else {
echo "Wrong Username or Password";
}
?>
and on my welcome page - page1.html i have included some php code:
<?php
session_start();
if(!session_is_registered(username)){
header("location:index.html");
}
?>
First off...dont store the password in the session. Thats just asking for trouble.
session_register("password");
Secondly....session_register() is a deprecated function and shouldn't be used anymore.
Instead do...
$_SESSION['username'] = $myusername;
Third....
header("location:page1.html");
Should be a PHP file if you want sessions to work across pages..
header("location:page1.php");
Then in that PHP page do...
session_start();
if(!isset($_SESSION['username'])){
header("location:index.php");
} else {
// Display stuff to logged in user
}
When I send data to my php file. Data is not getting stored in the database. My php code is below.
I am using one.com as my host. There I can use to php code using some components. So I gave an external link to the button to submit to php file.
But I cannot submit data to php code using button name or class attribute.
How can I store data in database using php file.
Please help me in this issue.Thank you in advance.
HTML:
<form method="post" action="register.php">
<div class="input-group">
<lable>Username</lable>
<input type="text" name="Username" required>
</div>
<div class="input-group">
<lable>Email</lable>
<input type="text" name="Email" required>
</div>
<div class="input-group">
<lable>Password</lable>
<input type="password" name="password_1" required></div>
<div class="input-group">
<lable>Confirm Password</lable>
<input type="password" name="password_2" required>
</div>
<p>Already a User?</p>
</form>
PHP:
<?php
$Username = "";
$Email = "";
$errors = array();
// connect to the database
$db = mysqli_connect('hostname', 'root', 'password', 'dbname');
echo "database connected";
// if the register button is clicked
$username = $_POST['Username'];
$email = $_POST['Email'];
$password_1 = $_POST['password_1'];
$password_2 = $_POST['password_2'];
echo "data is taken";
// if there are no errors, save user to database
$sql = "INSERT INTO Users(Username, Email, password) VALUES('$username',
'$email', '$password_1')";
mysqli_query($db, $sql);
echo "data inserted successfully";
?>
Is your database correct?
// connect to the database
$db = mysqli_connect('hostname', 'root', 'password', 'dbname');
echo "database connected";
And remember it always printed "database connected".
I am beginner in PHP.
My CODE
<?php
session_start();
$username = "ADMIN";
$host = "localhost";
$password = "chmuhammadsohaib123";
$database = "USER";
$con = mysqli_connect($host, $username, $password, $database);
$USERNAME = $_POST["lusername"];
$PASSWORD = $_POST["lpassword"];
if (isset($_POST["login"])) {
if (isset($_POST["loggedin"])) {
setcookie("RAUSERNAME", $USERNAME);
setcookie("RAPASSWORD", $PASSWORD);
}
$_SESSION["SRAUSERNAME"] = $USERNAME;
$_SESSION["SRAPASSWORD"] = $PASSWORD;
}
if (isset($_POST["login"])) {
$data = mysqli_query($con, "SELECT * FROM `INFO` WHERE `USERNAME` = '$USERNAME'");
if (mysqli_num_rows($data)>0) {
echo "<script type='text/javascript'>window.location.replace('../');</script>";
}
else {
print("<script type='text/javascript'>document.getElementsByClassName('errors').innerHTML = '<h1 class='redback'>SORRY, BUT THIS ACCOUNT DOESN'T EXISTS</h1>';</script>");
}
}
?>
MY HTML PAGE
<body>
<div class="errors"></div>
<fieldset class="replacement">
<legend>LOGIN</legend>
<h1>LOGIN WITH YOUR INFORMATION</h1><br><br>
<form method="POST" action="<?php $_SERVER["php_self"]; ?>">
<input type="text" name="lusername" placeholder="YOUR USERNAME">
<input type="password" name="lpassword" placeholder="YOUR PASSWORD" class="password">
<br>
<br>
<label>KEEP ME LOGGED IN: </label>
<input type="checkbox" name="loggedin" checked>
<br><br>
<input type="submit" name="login" value="LOGIN"></form>
</fieldset>
</div>
</body>
</html>
When I am changing innerHTML of errors as described above, it doesn't changes. It says ; is missing in console or sometimes that errors is null. How can I fix it?
At the point you echo your javascript code, the html element with the id errors dont exists inside the dom. So the return of getElementById will always be undefined.
<script>document.getElementById("errors")...</script>
... some more html
<div id="errors"></div>
You could fix this by calling the javascript code after the dom document is ready. Using jQuery, you could do this this way
// event handler for document ready
$(function() {
// at this point, the dom is ready and the 'errors' id exists
$('#errors').html("some error message");
});
This would work, but seems a little bit unnecessary. The better way would be to just echo the actual error message with php and don't use javascript to do this.
$error = false;
if (mysqli_num_rows($data)>0) {
header('location: ../');
} else {
$error = '<h1 class="redback">SORRY, BUT THIS ACCOUNT DOESN\'T EXISTS</h1>';
}
and later
<div class="errors">
<?php if ($error) echo $error; ?>
</div>
I'm trying to create a GUI to let the user edit any row that is displayed in my table. I've manage to create a form that pops up when the user clicks an image which symbolize an edit icon. Now I like to use Jquery (if possible) to fill this form with data from my DB. The error code is down bellow and I can't seem to get any results at all
Script
<script type="text/javascript">
$(document).ready(function(){
$('#edit').click(function() {
$.ajax({
url: 'edit.php?itemid=$itemid',
success: function(response) {
$('#itemid').val($itemid);
......
$('#status').val($status);
}
});
});
});
</script>
Edit.php
<?php
$DB = new mysqli("localhost", "root", "", "book1");
$result2 = mysqli_query($DB, "SELECT * FROM booking WHERE itemID='$itemid'");
while($row = mysqli_fetch_array($result2)){
$itemid = $row['itemID'];
......
$status = $row['status'];
}
echo (array($itemid, $userid, $description, $manufacturer, $model, $caldate, $duedate, $shelf, $status);
?>
Form
<div id="light1" class="white_content">
<form id="editform" name="myForm" action="checkout.php" method="POST">
<h2>Edit Instrument</h2>
<label>ItemID:</label>
<input type="text" id="itemid"/>
<br>
......
<a>Status: </a>
<input type="text" id="status"/>
<br>
<input type="submit" value="Accept">
<input href = "javascript:void(1)" onclick = "document.getElementById('light1').style.display='none';document.getElementById('fade').style.display='none'" type="reset" value="Close">
<br>
</form>
</div>
Error
ReferenceError: $itemid is not defined
$('#itemid').val($itemid);
change
$('#itemid').val($itemid);
to
$('#itemid').val('<?php echo $itemid; ?>');
I want to have server side validation. Is there a way how can I have the alert like this image whenever the user input the data like email that exist in my database, the alert will appear when I hit the sumbit button. Thanks in advance. :D
Here's my create_acc.html
<form action="create_acc.php" method="POST" id="fieldform">
<dl>
<p><dt>Email Address:</dt>
<dd>
<input type="text" name="e-mail" id="e-mail" class="text" placeholder="Your Email will be your log in" autocomplete="off">
</dd>
</p>
<p><dt>Create Password:</dt>
<dd>
<input type="password" name="password" id="password" class="text" placeholder="Remember your password" autocomplete="off">
</p>
<p>
<p><dt>Your Complete name:</dt>
<dd>
<input type="text" name="name" id="name" class="text" placeholder="Your Complete name" autocomplete="off">
</p>
<p>
<dt>
<input type="submit" value="Submit">
</dt>
</p>
</dl>
</form>
Here's my create_acc.php
<?php
session_start();
$email = $_POST['e-mail'];
$name = $_POST['name'];
$pass = $_POST['password'];
$hash = hash('sha256',$pass);
function createSalt(){
$text = md5(uniqid(rand(), true));
return substr($text,0,3);
}
$salt = createSalt();
$pass = hash('sha256',$salt.$hash);
$conn = mysqli_connect('localhost','root','','mydb');
$email = mysqli_real_escape_string($conn,$email);
$query = "INSERT INTO customers(Email,Password,Name,salt) VALUES('$email','$pass','$name','$salt')";
mysqli_query($conn,$query);
mysqli_close($conn);
header('Location: index.php');
?>
You could use ajax to do this. When the user submits the form you would send an ajax request with the form data to your php script, the script will then respond with a value that you use to deside if you should display an alert or not, here is a basic example using jquery:
$(document).ready(function() {
// catch submit events for your form
$('#your-form-id').submit(function() {
// make an ajax request to your validation script
$.ajax{(
url:'create_acc.php',
type:'POST',
data:$(this).serialize(),
dataType:'json',
success:function(response) {
if(!response.success) {
alert('Something went wrong!');
}
}
});
return false;
});
});
Then in your php script you return a code telling the client how it went:
// create_acc.php
// I assume the form only has one value 'email'
$success = false;
if(isset($_POST['email'])) {
// here you would check if the email already
// exists in the database
$query = "SELECT * FROM <table> WHERE email = ?";
$stmt = $con->prepare($query);
$stmt->bind_param('s', $_POST['email']);
// execute the query and check if a row was returned
}
// if everything went fine you should change success to true
// return json response to client
$response = array('success' => $success);
print json_encode($response);
exit;