Send a session variable from php to client side(HTML) - javascript

I am completely new to PHP and HTML. I want to send a session variable from PHP to client-side i,e HTML so that I can show which user is currently present.
If I just pass the variable to another php file present at the same location it does happen but I want to exchange the variable from server residing at local host to client at some other location Can any one help me in this
Thanks

let suppose you have a form in html
<form method="post" action="any.php">
<input type="text" name="fname" id="fname">
<input type="submit" value="Submit">
</form>
now in php you are getting input value
<?php
if isset($_POST["name"]){
$_SESSION["sessionname"] = $_POST["name"];
}
?>
so far you have taken input value from html and have stored in session with the name sessionname now you are going to show it in html again
<h1>welcome user : <?php echo $_SESSION["sessionname"]; ?> </h1>
you will need session_start(); on the very beginning of all php pages otherwise you will lose the scope of session variable.
for more information here is link wschools session

Related

Taking data from 1 website(input) and pasting it to another website(input)

I need to take a data of username, then check in db, which aunthefecator user has got and depends on aunthefecator send him the window where he can enter it and login in his account!
I have tryed to make it like a 1 form and the method was sent to another form but it doesn not work.
<form name="logon" action="check_login.php" method="POST" id="login">
<input type="text" name="username">
<input type="password" name="password">
<button type="submit">
<span>Log IN</span>
</button>
</form>
I just redirecting on this page without getting data.
You must put this code in top of your php page, or if you want to check login in another page , you should put this code in destination page that you want to check login authentication. Remember to put your code about checking user and pass from database.
<?php
if(isset($_POST[“username”] && $_POST[“password”]){
$user = $_POST[“username”];
$pass = $_POST[“password”];
//Check your db here and then replace your url that you want to reload client if user and pass was correct.
header(‘Location: http://yourURL.com’);
}else{
echo (“username or password was not correct”);
}
?>

grab values from input box with php

I was wondering if it would be possible to have a php file grab data from a html page with an input box, so that the user enters a word, and the php runs using that word in the script once the user hits enter. Any help would be appreciated.
Use a form with the method attribute set to ‘get’ or ‘post’ like this:
<form method=“post”>
<input type=“text” name=“test” value=“test” />
<button type=“submit”>submit</button>
</form>
Then on the same php page, use this to get the result, store it in a variable and output it:
<?php
$result = $_POST[‘test’];
echo $result;
?>
More information:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Your_first_HTML_form
Hope the formatting in this answer is ok, typed it out on my mobile phone.

Updating MySQL field beforeunload

I'm working on a CMS to allow users to access and classify records in MySQL database. I have created a user login system for this CMS. I am trying to program a lock out function that will monitor when a record is being viewed so that no other users can access this record. This is to prevent multiple users from trying to write to the same record at the same time.
So far this part works fairly well. When they access the record I update the field "in_use" within the MySQL database for that particular record to show that it is being viewed. When they have submitted the form with the classification for the record it, I update the field "in_use" to show that it is no longer being viewed.
The problem I have is when the user closes the browser without submitting the form, or when they navigate away from the page.
I have been trying to use AJAX to run an update on the beforeunload event, but it has not been working.
***** readRecord.php*****
<h1>Analyze</h1>
//PHP include that contains functions for php queries to the MySQL database.
<?php
include 'core/database/record_functions.php';
?>
//Form to search MySQL database by Case Number and Button to manually release a record that is in_use.
<form action="" method="post">
<p id="case_search">Search records by Case Number: <input type="text" id="case_num_field" name="case_num">
<input type="text" name="in_use" id="in_use" readonly="readonly" hidden="hidden" value="1">
<input type="text" name="user" id="user" readonly="readonly" hidden="hidden" value="<?php echo $_SESSION['username']; ?>">
<input type="submit" name="Submit" value="Select" id="case_num_submit">
<input type="Submit" name="release record" value="Release the current record" onclick="<?php update_in_use($_SESSION['case_num'], 0); ?>">
</p>
</form>
//Includes for jQuery library and AJAX code for automatically releasing the record on page navigation or close.
<script type="text/javascript" src="core/jquery-2.1.3.js"></script>
<script type="text/javascript" src="ajaxBridge.js"></script>
... code continues for reading records
... when the form is submitted update_in_use is called again to set the value to 0
***** ajaxBridge.js *****
// This code is supposed to run the query on releaseVariable.php to update the in_use field to 0 on the beforeunload event.
window.addEventListener("beforeunload", function(event){
if(xmlHttp.readyState==4)
{
xmlHttp.open("POST", "releaseVariable.php", true);
xmlHttp.send();
event.returnValue = "Hey it worked!";
}
event.returnValue = "Hey it failed!";
});
***** releaseVariable.php *****
// This simply runs the update_in_use function to set the in_use field to 0 for this particular case.
<?php
include 'core/init.php';
include 'read_record.php';
protect_page();
update_in_use($_SESSION['case_num'], 0);
?>
Am I going about this the right way? If not how do I address this issue instead?
If this is a good method, where did I go astray with the code?
I decided to abandon this approach and use php SESSION variables instead.

passing value from a php page to html page using javascript

I have a php page which displays certain profile data like Name,Username,Phone,Addres etc which were passed as params in url.Now after saving those details the page is being redirected to an html login page where I need to display the Username value in the text field for the Username.I tried to pass the username from php to html in many ways but in vain.By what means can i pass the username from the php page to html login page and display it in Username text field?
if you don't use any PHP Framework then you can save it by session or cookies. Then on login page you can get it and show
Use a php Site for login and than use this code at your registerscript (php) (be sure this is the first thing send to the client no echo before this)
header("LOCATION: http://www.something.com?username=walter&secoundusername=white");
In your login page you can access the data with
if(isset($_GET('username'))){
$username = $_GET('username');
}...
I hope this works for you.
your question is not too clear, however if the html login page is just a static page, i don't see how your request could be achieved.
If instead it's a php generated html page, then you may pass the variable via a hidden input field for example.
You could use the fputs php function to create a html page
<?php
$fp = fopen("file.html", 'w+');
fputs($fp, "YOUR HTML CODE");
fputs($fp, "YOUR HTML CODE");
fputs($fp, "$variable");
fclose($fp);
?>
<a href='file.html'>Link</a>
You can pass username as parameter in URL like
http://yoursite.com/login.html?username=abc
and in login.html use javascript to get the url parameter and assign it to textbox.
function getParamURL(param) {
return decodeURIComponent((new RegExp('[?|&]' + param + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}
var username = getParamURL('username');
document.getElementById("username").value= username;
suppose your textbox id is username then it should assign it to that text box
Here is the login page
<?php
if($_POST)
{
include("Connect.php");
$Username=$_POST["Username"];
$Password=$_POST["Password"];
$Query="select Username from user where username='$Username' and password='$Password' LIMIT 1";
$Result=mysql_query($Query);
while($R=mysql_fetch_array($Result))
{
header("location:main.html?username='$R[0]'");
}
}
?>
<form action="<?=$_SERVER['PHP_SELF'] ?>" method="POST">
<input type="text" name="Username">
<input type="text" name="Password">
<input type="submit"/>
</form>
Here is how you could retrieve the page.The variable needs to be a php page
<html>
<p>Welcome</p> <?php
$Name=$_GET["username"];
echo $Name;
?>
</html>

sending information into an IP through Html

I'm using making a web based microcontroller and now I want to send some sort of data to my microcontroller through a Html page .
can I do that in a raw Html file or I should use other programs like javascript or perhaps php
can every one give me some simple source code
Easiest way i could imagine is making a button, with a PHP script attached, and that PHP Execute a compiled binary that does something in your microcontroller.
Example:
index.html
<html>
<body>
<Form Name ="form1" Method ="POST" ACTION = "send.php">
First name: <input type="text" name="fname"><br>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Login">
</body>
</html>
send.php:
<?php
echo "<html><body>";
echo "The microcontroller say:"+exec('microcontrollerbinary $_POST['fname']');
echo "</body></html>";
?>
Your microcontroller should have a binary who read first argument from command line 'fname' and printout something

Categories

Resources