Hide form with php on submit? - php

I have a simple php - password protecton / login based on a form which works. PHP-code is in page "secure.php" and includes the html-file "accessed.html" when user + pass is correct.
But i want the form to hide when hidden page (accessed.html) is shown.
I have tried wrapping the form in a div and using javascript and display: none to hide, but it doesnt work - not locally or on server.
What am i doing wrong? It doesnt have to be js hiding the form after login..
PHP in top
<?php
$user = $_POST['user'];
$pass = $_POST['pass'];
if($user == "a"
&& $pass == "a")
{
include("accessed.html");
echo "<script>
document.getElementById('wrap').style.display = 'none';
</script>";
}
if(isset($_POST))
?>
And the form in the body:
<div id="wrap">
<form method="POST" action="secure.php">
User <input type="text" name="user"></input>
Pass <input type="text" name="pass"></input>
<input type="submit" name="submit" value="access page"></input>
</form>
</div>

Move the <script> to after wherever you display "#wrap"
If you want to do it on the server side (PHP) just use an if statement.

I think this is what you are looking for my friend
<html>
<body>
<div id="wrap">
<form method="POST" action="temp.php" target = "hidden">
<input type="text" name="user">User</input>
<input type="text" name="pass">Pass</input>
<input type="submit" name="submit" value="access page" onclick = "closediv()"></input>
</form>
</div>
<iframe name = "hidden" id = "hidden" style = "display: none"></iframe>
<script>
function closediv(){
document.getElementById("wrap").style.display = "none";
}
</script>
</body>
</html>
With the above code, you'll need to do your PHP processing on the current page. If this isn't doing what you want it to do and I misread your question, let me know and we can work on your predicament.

Related

Call php using arguments from a text box

I want to make a website that has a text box and if an user enters "hello" and clicks submit I want to show example.com/test.php?link=hello
How can I do that? The php is already written I just need to make something with html or php to be able to show example.com/test.php?link=whatUserEnters
Thanks!
The form should have the method "get" defined. Then on the test.php page you just
<form method="get" action="test.php">
...
<input type="text" name="link"/>
</form>
and on the test.php
<? echo $_GET['link']; ?>
or
<?= $_GET['link']?>
Edit with the POST solution:
<form method="post" action="test.php">
...
<input type="text" name="link"/>
</form>
and on the test.php
<? echo $_POST['link']; ?>
<!DOCTYPE html>
<html>
<body>
<!--just create a text box, give an id to it.-->
<input type="text" id="text_to_display" />
<input type="button" value="Submit" onClick="concat();" />
<!--Create a label or div assign a separate id to it too.-->
<p id="concatenated_text"></p>
<script>
//Create a function in javascript.
function concat() {
//Get the value in function using that id.
var text = document.getElementById("text_to_display").value;
//Concatenate the value with the string and put it in div using id.
var final_text = "example.com/test.php?link="+text;
// you can use location.href to use it for link purpose
location.href = final_text;
}
</script>
</body>
</html>

php post multiple submit buttons

First and foremost, this is my 1st time writing PHP code, so please forgive my newbie'isms.
I have a login form with 3 submit buttons (post method) named login, register and forgot. If I select login button, the login function in the PHP code gets called, however, the same is not true for the register and forgot buttons. Its almost like the only submit button that is working is the login buttons. My best guess at this point is there is id/name that not correct. For the sake of brevity I've removed the CSS portion. Any points in the right direction will be most appreciated.
<?php
function login(){
//do stuff
echo "Login";
}
function register(){
// do stuff
echo "Register";
}
function forgot(){
//do stuff
echo "Forgot";
}
if($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['login'])) {
login();
}
if(isset($_POST['register'])) {
register();
}
if(isset($_POST['forgot'])) {
forgot();
}
}
?>
<html>
<head>
<meta charset="utf-8">
<title>Login Page</title>
</head>
<body>
<div id="login">
<h1><strong>Welcome!</strong> Please login.</h1>
<form action="" method="post">
<fieldset>
<p><input type="text" name="username" required value="Username" onBlur="if(this.value=='')this.value='Username'" onFocus="if(this.value=='Username')this.value=''"></p>
<p><input type="password" name="password"></p>
<p><input type="submit" name= "login" value="Login"></p>
<p><input type="submit" name= "register" value="Register"></p>
<p><input type="submit" name= "forgot" value="Forgot Password"></p>
</fieldset>
</form>
</div> <!-- end login -->
</body>
</html>
HTML forms can not contain more than one input of type "submit" (which means others would not be functioning)
register and forgot are links rather than buttons.
Instead you could use:
Register
Another alternative is to use javascript to make these buttons act as links.
After copying and pasting my code into another file. It worked without any issues. The multiple submit buttons work without any issues.

Post Form Action to variable inside form

I have the following form. I want to change the form action to include the variables inside this form, 500pxuserid. Is there a way to do this? For the user experience, I want to prevent a re-direct. Currently, I get the variable on the other side, and then re-route them a new url, but this required a re-direct.
<form action="importphotos.php?view=500&mode=upload&page=1&u=&u2=&500pxuserid=" method="post">
<div style="width:100%;height:45px;background-color:rgb(2,173,234);">
<img style="width:30px;padding:10px;float:left;" src="https://photorankr.com/graphics/import_500pxicon.png" />
<h3 style="font-weight:300;font-size:20px;text-align:left;padding-left:40px;margin-top:8px;color:white;float:left;">Login to 500px</h3>
</div>
<div style="width:90%;height:150px;border-bottom:1px solid rgba(255,255,255);float:left;margin-top:-5px;">
<input style="outline:none;padding:8px;border-radius:3px;font-size:18px;width:263px;float:left;border:1px rgb(100,100,100);border-style:solid; margin:8px; margin-left:16px;" placeholder = "Enter your 500px id" type="text" name="500pxuserid" />
<input type="password" style="outline:none;padding:8px;border-radius:3px;font-size:18px;width:263px;float:left;border:1px rgb(100,100,100);border-style:solid; margin:8px; margin-left:16px;" placeholder = "Enter your 500px password" type="text" name="500pxuserpassword" />
<input class = "submitbox" type="submit">
</div>
</form>
</div>
Using javascript you could change the form action on form submit (onsubmit).
<script type="text/javascript">
function get_action(form) {
var user = document.getElementById('userid').value;
form.action = "importphotos.php?view=500&mode=upload&page=1&u=&u2=&500pxuserid=" + user;
}
</script>
<form action="" method="post" onsubmit="get_action(this);">
...
// add id=""userid" to input
<input ... type="text" name="500pxuserid" id="userid" />
</form>
Three things:
Add an id attribute to your form. For example, foo.
Add an onsubmit attribute to your form, onsubmit="foo()"
Add an id attribute to your 500pxuserid input. For example, bar.
It should look like this:
<form id="foo" onsubmit="foo();" ...
And your input:
<input id="bar" ...
And in your JavaScript add the following function:
function foo()
{
var form = document.getElementById('foo'),
bar = document.getElementById('bar');
form.action = 'importphotos.php?view=500&mode=upload&page=1&u=&u2=&500pxuserid=' + bar.value;
}
Also, keep in mind that this would only work if the user's browser has JavaScript enabled.

php javascript alertbox have to click 2 times before it show

I'm doing php that is textbox a value empty it will open a alertbox (I'm using javascript in here )
this is my code
<?php
include('config.php');
if(isset($_POST['submit'])){
$username=$_POST['username'];
?>
<script>
function validate(){
if(document.forms[0].username.value==""){
window.alert("You must enter both values");
return false;
}
}
</script>
<?php
}
?>
<html>
<div><p>Member Profile</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username<br>
<input class="user" type="text" name="username" id="username" /><br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</html>
The problem is i have to click 2 times before the alert show
please help me to solve this problem
It's because the script is inside the php if(isset){} block, one click submits the form, which generates the script and then it works the second time.. try this setup instead:
<?php
include ('config.php');
if (isset($_POST['submit']))
{
$username = $_POST['username'];
}
?>
<html>
<head>
<script>
function validate () {
if (document.forms[0].username.value == "") {
window.alert("You must enter both values");
return false;
}
}
</script>
</head>
<body>
<div>
<p>
Member Profile
</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username
<br>
<input class="user" type="text" name="username" id="username" />
<br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</body>
</html>
Edit:
I've moved the script tag inside the head tag. I'm not sure if there are any implications for having the script outside but just to be sure I've moved it.
2nd Edit: (OCD is kicking in)
I've added body tags, not sure if you copied and pasted this code but it looked weird to me :)

POST without redirect with PHP

I have a simple form for a mailing list that I found at http://www.notonebit.com/projects/mailing-list/
The problem is when I click submit all I want it to do is display a message under the current form saying "Thanks for subscribing" without any redirect. Instead, it directs me to a completely new page.
<form method="POST" action="mlml/process.php">
<input type="text" name="address" id="email" maxlength="30" size="23">
<input type="submit" value="" id="submit"name="submit" >
</form>
You will need AJAX to post the data to your server. The best solution is to implement the regular posting, so that will at least work. Then, you can hook into that using Javascript. That way, posting will work (with a refresh) when someone doesn't have Javascript.
If found a good article on posting forms with AJAX using JQuery .
In addition, you can choose to post the data to the same url. The JQuery library will add the HTTP_X_REQUESTED_WITH header, of which you can check the value in your server side script. That will allow you to post to the same url but return a different value (entire page, or just a specific response, depending on being an AJAX request or not).
So you can actually get the url from your form and won't need to code it in your Javascript too. That allows you to write a more maintanable script, and may even lead to a generic form handling method that you can reuse for all forms you want to post using Ajax.
Quite simple with jQuery:
<form id="mail_subscribe">
<input type="text" name="address" id="email" maxlength="30" size="23">
<input type="hidden" name="action" value="subscribe" />
<input type="submit" value="" id="submit"name="submit" >
</form>
<p style="display: none;" id="notification">Thank You!</p>
<script>
$('#mail_subscribe').submit(function() {
var post_data = $('#mail_subscribe').serialize();
$.post('mlml/process.php', post_data, function(data) {
$('#notification').show();
});
});
</script>
and in your process.php:
<?php
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'subscribe' :
$email_address = $_POST['address'];
//do some db stuff...
//if you echo out something, it will be available in the data-argument of the
//ajax-post-callback-function and can be displayed on the html-site
break;
}
}
?>
It redirects to a different page because of your action attribute.
Try:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="address" id="email" maxlength="30" size="23" />
<input type="submit" value="" id="submit" name="submit" />
</form>
<?php if (isset($_POST['submit'])) : ?>
<p>Thank you for subscribing!</p>
<?php endif; ?>
The page will show your "Thank You" message after the user clicks your submit button.
Also, since I don't know the name of the page your code is on, I inserted a superglobal variable that will insert the the filename of the currently executing script, relative to the document root. So, this page will submit to itself.
You have to use AJAX. But that requires JavaScript to be active at the users Brwoser.
In my opinion it's the only way to do without redirect.
to send a form request without redirecting is impossible in php but there is a way you can work around it.
<form method="post" action="http://yoururl.com/recv.php" target="_self">
<input type="text" name="somedata" id="somedata" />
<input type="submit" name="submit" value="Submit!" />
</form>
then for the php page its sending to have it do something but DO NOT echo back a result, instead simply redirect using
header( 'Location: http://yourotherurl.com/formpage' );
if you want it to send back a success message simply do
$success = "true";
header( 'Location: http://yourotherurl.com/formpage?success='.$success);
and on the formpage add
$success = $_GET['success'];
if($success == "true"){ echo 'Your success message'; } else { echo
'Your failure message';
Return and print the contents of another page on the current page.
index.php
<html>
<body>
<p>index.php</p>
<form name="form1" method="post" action="">
Name: <input type="text" name="search">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$_POST['search'];
include 'test.php';
}
?>
</body>
</html>
test.php
<?php
echo 'test.php <br/>';
echo 'data posted is: ' . $_POST['search'];
?>
Result:
Just an idea that might work for you assuming you have no control over the page you are posting to:
Create your own "proxy php target" for action and then reply with the message you want. The data that was posted to your php file can then be forwarded with http_post_data (Perform POST request with pre-encoded data). You might need to parse it a bit.
ENGLISH Version
It seems that no one has solved this problem without javascript or ajax
You can also do the following.
Save a php file with the functions and then send them to the index of your page
Example
INDEX.PHP
<div>
<?php include 'tools/edit.php';?>
<form method="post">
<input type="submit" name="disable" value="Disable" />
<input type="submit" name="enable" value="Enable" />
</form>
</div>
Tools.php (It can be any name, note that it is kept in a folder lame tools)
<?php
if(isset($_POST['enable'])) {
echo "Enable";
} else {
}
if(isset($_POST['disable'])) {
echo "Disable";
} else {
}
?>
Use
form onsubmit="takeActions();return false;"
function takeAction(){
var value1 = document.getElementById('name').innerHTML;
// make an AJAX call and send all the values to it
// Once , you are done with AJAX, time to say Thanks :)
document.getElementById('reqDiv').innerHTML = "Thank You for subscribing";
}

Categories