Update values without reloading using HTML and PHP - php

I would like to update the value of the first user with the second user without reloading, using HTML and PHP.
user.html
<html>
<head>
</head>
<h1>color</h1>
<body>
<div id="txtHint">
<form name="myForm" action="color.php" method="get">
<tr><td>color<input type='text' name='color' id='clr/></td></tr>
<tr><td><input type='submit' id='submit' name='submit' onclick=''/></td></tr>
</form>
</div>
</body>
</html>
color.php
<?php
$color=$_GET['color'];
?>
For example, user types "red" and therefore displays "red" in the PHP page:
user2.html
<html>
<head>
</head>
<h1>color</h1>
<body>
<div id="txtHint">
<form name="myForm" action="color.php" method="get">
<tr><td>color<input type='text' name='color' id='clr'/></td></tr>
<tr><td><input type='submit' id='submit' name='submit' onclick=''/></td></tr>
</form>
</div>
</body>
</html>
I want the output such that once user 2 types any color, the color of user 1 also gets changed to the user2 color. How can I do that?

You will need ajax to do this.
Only echo in php page can do nothing. You need to save the color to a database.
In the user.html page you should check periodically for changes in the database. And in the user2.html page you need to update the database using ajax or do a normal post or get method.

Related

Is it possible to place two forms and save two cookie values in PHP?

I'm wondering if it would be possible to place two forms on the same page
and saving their cookie values respectively after clicking submit buttons.
With code below, when I click submit button on the first form, its cookie value is saved successfully but when I click on the second form, second value is saved but the first value is overwritten.
<?php
setcookie('username[user111]', $_POST['user111'], time()+60);
setcookie('username[user222]', $_POST['user222'], time()+60);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>name:<?php echo $_POST['user111']; ?></p>
<form method="POST" action="">
<input type="hidden" name="user111" value="TOM">
<input type="submit" value="close">
</form>
<p>name:<?php echo $_POST['user222']; ?></p>
<form method="POST" action="">
<input type="hidden" name="user222" value="BOB">
<input type="submit" value="close">
</form>
</body>
</html>
I'd like to save both
Name:username[user111] Value:BOB
Name:username[user222] Value:TOM
If I could save the values respectively, it'd not necessary to use form submit
but if possible I'd like to use PHP instead of JavaScript.
Any advice would be appreciated.

Getting post data from form with iframe doesn't work PHP

I have this code:
<!DOCTYPE html>
<html>
<body>
<iframe name="votar" style="display:none;"></iframe>
<form id="excel form" method="post" target="votar">
<input type="submit" name="test" id="test" value="RUN" /><br/>
</form>
</body>
</html>
<?php
if(isset($_POST['test']))
{
echo "hello world";
}
?>
what am I trying to do? well I try to get hte post data from this form without reloading the page and without using ajax, but what am I doing wrong? I tried looking around, but all the other solutions are to long or just not prectical for my website. please help.
EDIT
just changed submit to test, doesn't matter.
<form action="" method="post" >
<!-- code -->
</form>

Linking html to php with $_POST

I have been trying to create a form that reads a post from an HTML form and displays an element from that post IF it detects that the post exists.
However, each time the post is submitted, it simply reloads the form as though no post were provided.
<!DOCTYPE html>
<html>
<head>
<title>Upload from Manifest</title>
</head>
<body>
<?php
if (isset($_POST['manifest'])) {
echo 'we are in the IF';
echo($_POST['manifest']);
}
?>
<h1>Submission from manifest into main db</h1>
<div class="container offset-top120">
<form method="post" action="https://nhsggc.cogiva.com/prism/loadFromManifest.php" enctype="multipart/form-data">
<input id="manifest" type="text" />
<input id="submit" value="Submit" type = "submit" />
</form>
</div>
</body>
</html>
Your form is going to either a different page (https://nhsggc.cogiva.com/prism/loadFromManifest.php so check for that first) if you wanted it to go to same page, you can give the action as just '#', or put in the whole URL like you have.
You're missing the name attribute from your submit input and text input. Read up on the name attribute!
<input id="manifest" type="text" name="manifest">
<input id="submit" value="Submit" type="submit" name='submit' />
Then your PHP should look like this:
<?php
if (isset($_POST['submit'])) {
echo 'Inside an if';
echo $_POST['manifest'];
}
Then it should work.

How do i display the ouput in the same PHP page

I have written a code and now i want to view the output of user selected radio button in the same php page?
How can i do it? Here is my code
<html>
<div>
<form>
<input type='submit' name='radiotest' value='1crn' /> </div>
<input type='submit' name='radiotest' value='1FFZ' /> </div>
</form>
</div>
<div>
<head>
<script src="jmol/Jmol.js"></script>
</head>
<script>
jmolInitialize("jmol");
</script>
<script>
jmolSetAppletColor("gray");
jmolApplet(450, "load <?php echo "/jmol/".$_REQUEST['radiotest'].".pdb"; ?>; select all; cartoon on; wireframe off; spacefill off; color chain; ");
</script>
</div>
</html>
Any suggestions?
This should not be done using PHP (javascript would be a better candidate), but to answer the question:
Make the form so it has an action and method attributes. Action will be the current page (you don't need to specify this attribute if it's the same page) and method will be GET or POST.
Something like this:
<form action="" method="GET">
Now, when you click the button, the form gets submitted and the page reloads but the URL is different:
http://site.com/page.php?radiotest=1crn
You can now check to see if the $_GET['radiotest'] is set using isset() function. If it is set, do whatever you want it to do.
isset($_GET['radiotest']) - tells you if radiotest=something is in the URL
$_GET['radiotest'] - gives you the value of "something" from radiotest=something in the URL.
Because PHP is a server-side language, all code gets executed on the server. By the time the page gets sent to the browser, it has already executed and no further computations can be made. If you must have a PHP solution, the request must be sent to the server, the page must reload.
This is the one of the way to display the output in the same page...
<html>
<head>
</head>
<body>
<form name='test' method='GET' action="">
<input type='radio' name='radiotest' value='1crn' />1crn
<input type='radio' name='radiotest' value='1FFZ' />1FFz
<input type='submit' value='submit' name='submit' />
</form>
<?php
if(isset($_GET("submit")))
{
echo $_GET["radiotest"];
}
?>
</body>
</html>

Submit button clicking changing the value of it to another thing

maybe very easy!
I'm php coder and I don't have experience in js but I must do this for one of my codes
suppose I have sub1 in page after clicking it must be that sub1 but value now is sub2
<html>
<head>
<title>pharmacy</title>
</head>
<body>
<form method="post" action="pharmacy.php">
<?php
//some code
if(array_key_exists('update',$_POST)){
//somecode
}
?>
<input type="submit" name="update" value="<?php echo if(isset($_GET['update'])) ? 'Show' : 'Update' ?> ">
</form>
</body>
</html>
show as function name does not really make sense here (imo), but you could do:
<input type="submit" name="sub" value="sub1" onclick="show(this)">
and
function show(element) {
element.value = 'sub2';
}
Important:
But that will actually not solve your problem. As soon as you click the button, the form is submitted, meaning the browser initiates a new request and will load a new page. So every change you made the current page is lost anyway.
The question is: What are you trying to do?
It seems to me that you should change the value of the button on the server side. You have to keep track which form was submitted (or how often, I don't know what you are trying to do) and set the value of the button accordingly.
Update:
I see several possibilities to solve this:
You could keep using JavaScript and send and get the data via Ajax. As you have no experience with JavaScript, I would say you have to learn more about JavaScript and Ajax first before you can use it.
You could add a GET parameter in your URL with which you can know which label to show for the button. Example:
<form method="post" action="?update=1">
and
<input type="submit" name="sub" value="<?php echo isset($_GET['update']) ? 'Show' : 'Update' ?> ">
Similar to 2, but use a session variable (and not a GET parameter) to keep track of the state.
Update2:
As you are already having $_POST['update'] you don't need the URL parameter. It could just be:
<html>
<head>
<title>pharmacy</title>
</head>
<body>
<form method="post" action="pharmacy.php">
<input type="submit" name="update" value="<?php echo isset($_POST['update']) ? 'Update' : 'Show'; ?> ">
</form>
</body>
</html>
This should do it
function show(){
document.getElementsByName('sub')[0].value = 'sub2';
return false;
}
Edit: if you don't want it to submit the form, just add a return false, but then you'd need to change your onclick from your submit button to your forms onsubmit;
<html>
<head>
<title>test</title>
<script>
function show()
{
document.getElementById("sub").value= "sub2";
return true;
}
</script>
</head>
<body>
<form method="post">
<input type='submit' id="sub" name='sub' value="sub1" onclick="return show()">
</form>
</body>
</html>

Categories