I want to call a function from an included file when a form is pressed:
<?php include 'app/lc.php'; $lc = new lc();?>
<form action="<?php $lc->create_user('1', '2'); ?>">
Email:<br>
<input type="text" name="lastname">
<br>
Password:<br>
<input type="text" name="lastname">
<br>
<input type="submit" value="Submit">
</form>
above I try to create an instance of the lc class and then run the create user function in that class.
However, this results in my code breaking at the action tag in the form. What am I doing wrong and how do I fix it?
You can't call a PHP function from a form's action tag. You need to put it in another file and then call it or call that other file using AJAX
E.G.
Form.php
<form action="createuser.php" method="post">
Email:<br>
<input type="text" name="email">
<br>
Password:<br>
<input type="text" name="password">
<br>
<input type="submit" name="cu" value="Submit">
</form>
CreateUser.php
<?php
include 'app/lc.php';
$lc = new lc();
if(!empty($_POST['submit']){
$lc->create_user('1', '2');
// or $_POST['password'], $_POST['email'], etc.
}
Related
I am building a WordPress plugin for my livechat. When someone downloads the plugin, I want them to fill out some information (name, e-mail, etc). After submitting that info, the form has to disappear/hide. For some reason I am not successful and imo I've tried everything. At the moment I'm trying to do it with an if-statement checking if the submit-button isset(). Unfortunately that didn't work.
Can someone please help me? The code for display the form and the page after submitting:
<?php
public function display_plugin_setup_page()
{
if (isset($_POST['submitForm'])) {
?>
<form action="options.php" method="post">
<?php
settings_fields('mister_chat_options');
do_settings_sections($this->plugin_name); ?>
<input name="submit" class="button button-primary" type="submit" value="<?php esc_attr_e('Save'); ?>" />
</form>
<?php
} else {
// create the form
?>
<form method="post" action="sendmail.php">
<input type="hidden" name="formSent">
<fieldset>
<input placeholder="Voornaam" type="text" id="vnaam" name="vnaam">
</fieldset>
<fieldset>
<input placeholder="Achternaam" type="text" id="anaam" name="anaam">
</fieldset>
<fieldset>
<input placeholder="Bedrijfsnaam" type="text" id="bnaam" name="bnaam">
</fieldset>
<fieldset>
<input placeholder="E-mailadres" type="email" id="email" name="email">
</fieldset>
<fieldset>
<input placeholder="Telefoonnummer" type="tel" id="telef" name="telef">
</fieldset>
<fieldset>
<input type="submit" name="submitForm" id="contact-submit" data-submit="...Verzenden">
</fieldset>
</form>
<?php
}
}
I placed the sendmail.php file inside the file above and that fixed my problem.
I have a simple signup form:
<h2>Signup</h2>
<form action="actions.php">
Email:<br>
<input type="text" name="lastname">
<br>
Password:<br>
<input type="text" name="lastname">
<br>
<br>
<input type="submit" name="signupButton" value="submit">
</form>
And here is my actions.php:
<?php
echo 'test1';
if(isset($_POST['signupButton'])){
echo 'test2';
}
?>
But when I click the submit button the page that shows is only shows:
test1
Why is the button being clicked failing to pass the if statement?
you need to specify a method type (post/get)
<form action="actions.php" method="POST">
I want to place both register and login form on the same page.
They both starts with:
if (!empty($_POST)) ...
so, I need something like:
if (!empty($_POST_01))... // regForm
and
if (!empty($_POST_02))... //loginForm
Also how to prevent executing first form if the second is busy, and vice versa (user clicks on both)
My idea is to create a simple variable on starting process, for example $x = 1 and at the end of process $x = 0, so:
if ((!empty($_POST_01)) And $x = 0)...
Probably, there is a better way.
You could make two forms with 2 different actions
<form action="login.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
<br />
<form action="register.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="Register">
</form>
Or do this
<form action="doStuff.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="hidden" name="action" value="login">
<input type="submit" value="Login">
</form>
<br />
<form action="doStuff.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="hidden" name="action" value="register">
<input type="submit" value="Register">
</form>
Then you PHP file would work as a switch($_POST['action']) ... furthermore, they can't click on both links at the same time or make a simultaneous request, each submit is a separate request.
Your PHP would then go on with the switch logic or have different php files doing a login procedure then a registration procedure
Well you can have each form go to to a different page. (which is preferable)
Or have a different value for the a certain input and base posts on that:
switch($_POST['submit']) {
case 'login':
//...
break;
case 'register':
//...
break;
}
Give the submit buttons for both forms different names and use PHP to check which button has submitted data.
Form one button - btn1
Form two button -btn2
PHP Code:
if($_POST['btn1']){
//Login
}elseif($_POST['btn2']){
//Register
}
You can use this easiest method.
<form action="validator.php" method="post" id="form1">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="submit" form="form1">
</form>
<br />
<form action="validator.php" method="post" id="form2">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="submit" form="form2">
</form>
Here are two form with two submit button:
<form method="post" action="page.php">
<input type="submit" name="btnPostMe1" value="Confirm"/>
</form>
<form method="post" action="page.php">
<input type="submit" name="btnPostMe2" value="Confirm"/>
</form>
And here is your PHP code:
if (isset($_POST['btnPostMe1'])) { //your code 1 }
if (isset($_POST['btnPostMe2'])) { //your code 2 }
Hope this will help you. Assumed that login form has: username and password inputs.
if(isset($_POST['username']) && trim($_POST['username']) != "" && isset($_POST['password']) && trim($_POST['password']) != ""){
//login
} else {
//register
}
I have the following form:
<FORM action="http://url.to.mysite.com/doc.php" method="post">
<INPUT type="text" id="name">
<INPUT type="text" id="id">
<INPUT type="submit" value="Send">
</P>
</FORM>
Unfortunately, whatever is typed into the text fields, the PHP script receives only empty strings in every $_POST variable. All of the variables are set, just empty. What can be the cause of that?
You are missing the name properties in your html:
<FORM action="http://url.to.mysite.com/doc.php" method="post">
<INPUT type="text" name="name" id="name">
<INPUT type="text" name="id" id="id">
<INPUT type="submit" value="Send">
</FORM>
Only form elements with a name attribute will be sent to the PHP script (same with POST)
Try this:
<form action="http://url.to.mysite.com/doc.php" method="post">
<input type="text" id="name" name="name">
<input type="text" id="id" name="id">
<input type="submit" value="Send">
</form>
You need to add the name attribute to your input tags. Example: <input type="text" name="name" id="name" />
I'm trying to create a BMI calculator. This should allow people to use either metric or imperial measurements.
I realise that I could use hidden tags to solve my problem, but this has bugged me before so I thought I'd ask: I can use $_POST['variableName'] to find the submitted variableName field-value; but...I don't know, or see, how to verify which form was used to submit the variables.
My code's below (though I'm not sure it's strictly relevant to the question):
<?php
$bmiSubmitted = $_POST['bmiSubmitted'];
if (isset($bmiSubmitted)) {
$height = $_POST['height'];
$weight = $_POST['weight'];
$bmi = floor($weight/($height*$height));
?>
<ul id="bmi">
<li>Weight (in kilograms) is: <span><?php echo "$weight"; ?></span></li>
<li>Height (in metres) is: <span><?php echo "$height"; ?></span></li>
<li>Body mass index (BMI) is: <span><?php echo "$bmi"; ?></span></li>
</ul>
<?php
}
else {
?>
<div id="formSelector">
<ul>
<li>Metric</li>
<li>Imperial</li>
</ul>
<form name="met" id="metric" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Kilograms">kg</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (<abbr title="metres">m</abbr>):</label>
<input type="text" name="height" id="height" />
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<form name="imp" id="imperial" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Pounds">lbs</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (Inches):</label>
<input type="text" name="height" id="height" /
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<?php
}
?>
I verified that it worked (though without validation at the moment -I didn't want to crowd my question too much) with metric; I've added the form but not the processing for the imperial yet.
To identify the submitted form, you can use:
A hidden input field.
The name or value of the submit button.
The name of the form is not sent to the server as part of the POST data.
You can use code as follows:
<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>
You can do it like this:
<input type="text" name="myform[login]">
<input type="password" name="myform[password]">
Check the posted values
if (isset($_POST['myform'])) {
$values = $_POST['myform'];
// $login = $values['login'];
// ...
}
The form name is not submitted. You should just add a hidden field to each form and call it a day.
In the form submitting button (id method of form is post):
<input type="submit" value="save" name="commentData">
In the PHP file:
if (isset($_POST['commentData'])){
// Code
}
For some reason, the name of the submit button is not passed to the superglobal $_POST when submitted with Ajax/jQuery.
Use a unique value on the submit button for each form like so
File index.html
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="contact">Send Message</button>
</form>
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="support">Send Message</button>
</form>
File email.php
<?php
if (isset($_POST["submit"])) {
switch ($_POST["submit"]) {
case "contact":
break;
case "support":
break;
default:
break;
}
}
?>
As petervandijck.com pointed out, this code may be susceptible to XSS attacks if you have it behind some kind of log-in system or have it embedded in other code.
To prevent an XSS attack, where you have written:
<?php echo "$weight"; ?>
You should write instead:
<?php echo htmlentities($weight); ?>
Which could even be better written as:
<?=htmlentities($weight); ?>
You can use GET in the form's action parameter, which I use whenever I make a login/register combined page.
For example: action="loginregister.php?whichform=loginform"
I had a similar problem which brought me to this question. I reviewed all the preceding answers, but ultimately I ending up figuring out my own solution:
<form name="ctc_form" id="ctc_form" action='' method='get'>
<input type="hidden" name="form_nm" id="form_nm">
<button type="submit" name="submit" id="submit" onclick="document.getElementById('form_nm').value=this.closest('form').name;">Submit</button>
</form>
It seamlessly and efficiently accomplishes the following:
Passes the form name attribute via a hidden input field, without using the fallible value attribute of the submit button.
Works with both GET and POST methods.
Requires no additional, independent JavaScript.
You could just give a name to the submit button and do what needs to be done based on that. I have several forms on a page and do just that. Pass the button name and then if button name = button name do something.
Only the names of the form fields are submitted, but the name of the form itself is not. But you can set a hidden field with the name in it.