Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I hope you can help. I'm a new to PHP and it is driving me crazy!
I have a html document with separate login and registration forms. Each one of these has it's own php script to either register or to login. When testing the input error messages for either the login or registration forms it seems to run both scripts and I get the error messages for both.
I have spent most of today trying to find a solution to this but to no avail. Is there a way that I can define a name to each script so I can add an action to each form tags referring to the particular php script?
Or This there a way of using a php if else statement based on which html button is pressed?
Thank you in advance
Hopeless coder
Or This there a way of using a php if else statement based on which
html button is pressed?
Yes, assuming you have
<input type='submit' name='subbtn' value='Register'>
...
<input type='submit' name='subbtn' value='Log In'>
Then in php:
if ($_REQUEST['subbtn'] == 'Register') {
// they pressed register
} else {
// they pressed log in (or some other submit button)
}
You could attach a hidden element to your post method
<input type="hidden" name="type" value="login">
or
<input type="hidden" name="type" value="register">
The above should be in respective forms.
On the PHP page
<?
if($_POST['type'] == "login") {
// continue login operation
} else {
// do registration
}
?>
Sure there's a way to separate them into two files and then call for action separately.
<form action="registration.php">
...
</form>
<form action="login.php">
...
</form>
or there's another way to do it in one document
<form action="" method="POST">
...
<input type="submit" name="btn_register">
</form>
<form action="" method="POST">
...
<input type="submit" name="btn_login">
</form>
<?php
if(isset($_POST['btn_register'])) {
//Do the stuff with registration
}
if(isset($_POST['btn_login'])) {
//Do the stuff with login
}
?>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So I have a page with 2 different PHP code
example;
<?php
blablabla...
?>
<?php
testtestest...
?>
and I have 1 form with 2 submit buttons but I dont know how I can link 1 button to execute php code 1 and the other button to execute php code 2.
Now the 2 buttons only execute php code 1 which is not what i want.
Give a name to your buttons, and check the name which is submitted :
<?php if (isset($_POST['submit_1'])) { ... } else if (isset($_POST['submit_2'])) { ... } ?>
<form action="" method="post">
<input type="submit" name="submit_1">
<input type="submit" name="submit_2">
</form>
First answer:
create two seperate forms
<form action="page1.php">
<input type=button name=button1>
</form>
<form action="page2.php">
<input type=button name=button2>
</form>
Answer after OP commented:
use a "hidden" field in HTML which will contain the button which is pressed (keep it simple and use a single number like 1 is button 1 and 2 is the other button)
You put a javscript function on both buttons that will change the value of this field when being pressed.
In your PHP Script page where you land if you press the button you just do
if ($_POST[hiddenfield]>1)
{//button1 is pressed
}else
{//button2 is pressed
}
if you need help with the hidden field or javascript, comment and I'll show you the code
Im new to coding and I really need help. I have a PHP form that I want to redirect to another PHP file after hitting submit and all the required fields are answered correctly. The redirecting is not working without jeopardizing the form file. Sorry for dumb question but please help and If you could also tell me where to put the code, I've tried:
< ?php header("Location: http://www.redirect.to.url.com/"); ?>
but then It redirects immediately as you enter the form page.
you can use onsubmit Event it makes you able to submit the inputs or not, check this code:
<form action="/action_page.php" onsubmit="return myFunction()">
Enter name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
<script>
function myFunction() {
//your code or process
return true; //if fields are answered
return false; //if fields are notanswered
}
</script>
I didn't understand you well what yo mean by fields must be are answered.You mean correct or not empty? if not empty u can use required attribute
<form action="/action_page.php" onsubmit="return myFunction()">
Username: <input type="text" name="usrname" required>
<input type="submit">
</form>
if not you can check if fileds correct by checjing them by js and don't forget to return true so the php file can run.
Hope this help if not post your code
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In the following code, I would like to get the input, create the page, then go to the redirected page.
But instead I am getting the input, then going to the redirected page (it's skipping creating the page).
How can I achieve the first one?
<?php
$newfile = $_GET['abc'];
$file = 'example.com';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
<form onsubmit="location.href='http://www.example.com/file-created;">
<form name="form" action="" method="get">
<input type="text" name="abc" placeholder="Type Here. . .">
<input type="submit" value="Go">
</form>
</form>
I tried using echo, but it was of no use (maybe I am making a mistake), and when I input something (without redirect), my url changes to example.com?abc=[input_here]. Why?
When you don't have the onsubmit, it is submitting the form to the server to the same page that is currently loaded because you don't have a value for the action="" set. So if all of that code was in index.php, the form will by default submit to index.php because you didn't tell it go to anywhere else.
Since you are using method="get", it is going to add all the form data as a query string to the action (which you didn't specify, so it adds it to the current url). So it appends abc=(whatever input) to your url and submits the form.
Now, when you have the onsubmit event in the url, instead of submitting the data to the server, it is just redirecting to that url. Nothing is going to happen with all of the form data that was entered. It is basically thrown away.
What you need to do is have your code submit to your server, do whatever processing you need to do, and then redirect to the url you want.
So something like:
<?php
// check if "abc=(whatever)" is in the url, and if so do the copy to, otherwise skip it
if(isset($_GET["abc"])) {
$newfile = $_GET['abc'];
$file = 'example.com';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
} else {
header("Location: http://yoursite.com/" . $newfile);
exit;
}
}
?>
<form name="form" action="" method="get">
<input type="text" name="abc" placeholder="Type Here. . .">
<input type="submit" value="Go">
</form>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
When I click in my newsletter subscribe button in my footer Im going to the top of my page.
I can have my sucess message my error messages but Im going always to the top of my page. And I dont want this because I want that usre see the feedback message without scroll down to my footer.
Do you see why this happening and how can I fix this?
<form action="" name="newsletter" method="post" enctype="multipart/form-data">
<div id="email_newsletter_container">
<input type="text" id="email" value="<?php if(isset($_POST['email'])) echo $email; ?>" name="email" placeholder="Insert your email..." required/>
<input type="hidden" name="newsl_sub" value="subscribe" />
<button class="btnn" type="submit" name="subscribe"><span>Subscribe</span></button>
</div>
</form>
When you put action="" the action will reload the page. If you put action="#newsletter" it should take the user to the section of the page that contains the form when the form is submitted. You may also want to add id="newsletter" to the form to ensure compatibilities.
<? if($post_was_submitted_and_comment_was_inserted) { ?>
<script>
window.addEventListener("load", function(){
document.getElementById("comments").scrollIntoView();
});
</script>
<? } ?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm curious about this kind of behavior. Suppose the code below:
<form name="myfrom" action="" method="post">
Username: <input type="text" name="user" id="username" />
<input type="submit" name="submit_form" value="Submit" />
</form>
<?php
if($_POST['submit_form'] == "Submit") {
echo "do something";
}
?>
What is the reason it works? Is that because when you click on the submit button the page reloads again and then the PHP script runs again? Is there an explanation?
If you mean, how to test when the form is submitted try this:
<form name="myfrom" action="" method="post">
Username: <input type="text" name="user" id="username" />
<input type="submit" name="submit_form" value="Submit" />
</form>
<?php if(isset($_POST['user'])) {
echo "do something";
}
?>
and if you test if($_POST['submit_form'] == "Submit") you get true.. i.e 1...
When ever you submit any PHP form with POST method then the following happens behind the scenes:
The server takes the values of all the HTML input elements and takes their names too...
Then the server, puts the names and values in the POST array in the following fashion:
{'name1'=>'value1','name2'=>'value2','name3'=>'value3','name4'=>'value4'...'name_n_'=>'value_n_'}
You change the value of a text field by entering some text into it. However, the user can't really change the value of a submit button, i.e. the displayed text on a submit button, hence whenever you say echo $_POST['submit_form'] you will always get the value that you set in HTML i.e. Submit
hope that helps...