multiple forms on a single PHP page - php

Im having issues with a ''profile'' page where users will be able to change their username, email, name, password and so on but it seems to have conflicts when i have more than 1 form on the page as they all work individually?
I could be missing something obvious so if anyone could help id much appreciate it.
Base
<?php
require 'core/init.php';
include 'includes/overall/header.php';
?>
<h1><p>Hello <?php echo escape($user->data()->username); ?></a>!</p></h1>
<h4><p>You joined the MMOunition community <?php echo escape($user->data()->joined); ?></a></p></h4>
<alert_banner>
<?php
if(Session::exists('home')) {
echo '<p>', Session::flash('home'), '</p>';
}
?>
</alert_banner>
<br />
<form action="changeusername.php" method="post">
<div class="field">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="<?php echo escape($user->data()->username); ?>">
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
<br />
<form action="changepassword.php" method="post">
<div class="field">
<label for="password_current">Current password:</label>
<input type="password" name="password_current" id="password_current">
</div>
<div class="field">
<label for="password_new">New password:</label>
<input type="password" name="password_new" id="password_new">
</div>
<div class="field">
<label for="password_new_again">New password again:</label>
<input type="password" name="password_new_again" id="password_new_again">
<input type="submit" value="Change">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
</br>
<form action="changename.php" method="post">
<div class="field">
<label for="name">Name:</label>
<input type="text" name="name" id="name" value="<?php echo escape($user->data()->name); ?>">
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
<br />
<form action="changeemail.php" method="post">
<div class="field">
<label for="email">Change email address:</label>
<input type="text" name="email" id="email" value="<?php echo escape($user->data()->email); ?>">
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
<?php
include 'includes/overall/footer.php';
?>

Hello You haven't mentioned what problem you actualy facing but this seems like the problem is the unique identification of a form at server side
Please see this post
Multiple HTML Forms on One Page
Hope this will help ;)

Have each form action set to the current page, and use hidden inputs to determine what action is being taken. Using this method will prevent the user from being dropped onto a different page, and puts the action being taken into a variable that you can use or manipulate.
If you want to have separate PHP scripts for actual functions, use includes or requires.
<form action="" method="post">
<div class="field">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="<?php echo escape($user->data()->username); ?>">
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<input type="hidden" name="action" value="changeUsername">
</div>
</form>
Then the PHP script gets the action from the POST and uses a switch to determine which functions to call.
if (isset($_REQUEST['action'])) {
$action = $_REQUEST['action'];
switch ($action) {
case "changeUsername":
changeUsername();
break;
case "changePassword":
...
As mentioned, if you want to have these functions in separate php files, you can use include or require.
This conveniently drops the user right back on the same page when they update information, so that they can make additional updates.

Related

On form submit, pass email address from first form to second form

I have two forms, the first gets submitted then the page reloads and shows the second. The problem I have is passing the email address from the first form to the second one. What's the best way to do this without interfering with my actions or changing it from POST to GET?
<?php if(!isset($_POST['crowd_email'])){ ?>
<form id="form" method="POST" action="<?php require( COMMON_PATH . '/components/crowdfunding.php' ); ?>">
<input type="text" name="crowd_email" class="crowd_email" value="" placeholder="Email address">
<input type="submit" class="register-btn" value="Register Interest" name="submit">
</form>
<?php } else { ?>
<form id="form" method="POST" action="<?php require( COMMON_PATH . '/components/crowdfunding-extra.php' ); ?>">
<input type="text" name="first_name" class="crowd_email extra-info" value="" placeholder="First name">
<input type="text" name="tel_no" class="crowd_email extra-info" value="" placeholder="Telephone number">
<input type="text" name="amount" class="crowd_email extra-info" value="" placeholder="Amount to invest">
<input type="submit" class="register-btn" value="Register Interest" name="submitextra">
</form>
<?php } ?>
1) Add a hidden field to the second form.
2) Add the value of the email address (submitted from the first form) to the hidden field when rendering the second form.
e.g.
<?php if(!isset($_POST['crowd_email'])){ ?>
<form id="form" method="POST" action="<?php require( COMMON_PATH . '/components/crowdfunding.php' ); ?>">
<input type="text" name="crowd_email" class="crowd_email" value="" placeholder="Email address">
<input type="submit" class="register-btn" value="Register Interest" name="submit">
</form>
<?php } else { ?>
<form id="form" method="POST" action="<?php require( COMMON_PATH . '/components/crowdfunding-extra.php' ); ?>">
<input type="text" name="first_name" class="crowd_email extra-info" value="" placeholder="First name">
<input type="text" name="tel_no" class="crowd_email extra-info" value="" placeholder="Telephone number">
<input type="text" name="amount" class="crowd_email extra-info" value="" placeholder="Amount to invest">
<input type="hidden" name="email" value="<?php echo $_POST["crowd_email"]; ?>" />
<input type="submit" class="register-btn" value="Register Interest" name="submitextra">
</form>
<?php } ?>
That way, when you submit the second form, the email field will be submitted along with the rest of the new data.
You can just add a hidden input in the second form containing the email value and give it the value from the first submission
<?php if(!isset($_POST['crowd_email'])){ ?>
<form id="form" method="POST" action="<?php require( COMMON_PATH . '/components/crowdfunding.php' ); ?>">
<input type="text" name="crowd_email" class="crowd_email" value="" placeholder="Email address">
<input type="submit" class="register-btn" value="Register Interest" name="submit">
</form>
<?php } else { ?>
<form id="form" method="POST" action="<?php require( COMMON_PATH . '/components/crowdfunding-extra.php' ); ?>">
<!-- New line to hold the email invisbly -->
<input type="hidden" name="crowd-email" value="<?php echo $_POST['crowd_email']; ?>"/>
<input type="text" name="first_name" class="crowd_email extra-info" value="" placeholder="First name">
<input type="text" name="tel_no" class="crowd_email extra-info" value="" placeholder="Telephone number">
<input type="text" name="amount" class="crowd_email extra-info" value="" placeholder="Amount to invest">
<input type="submit" class="register-btn" value="Register Interest" name="submitextra">
</form>
<?php } ?>

unable to get value of form with $_post

i want to get value of input of my form via $_post in my insert page but my problem is that i get error massage:undefined index:track_code and i cant get value of it.the names is same what is problem?
this is my problem why stackoverflow want more detail. this is all detail
<form action="insert.php" method="post">
<input name="track_code" type="text" class="tiny-size" value="<?php echo $track_code_rnd ; ?>" style="width: auto;height: auto" disabled />
</form>
<form action="insert.php" method="post" class="form-horizontal form-checkout">
<div class="control-group">
<label class="control-label" for="firstName">نام<span class="red-clr bold">*</span></label>
<div class="controls">
<input name="first_name" type="text" id="firstName" class="span4" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="lastName">نام خانوادگی<span class="red-clr bold">*</span></label>
<div class="controls">
<input name="last_name" type="text" id="lastName" class="span4">
</div>
</div>
<input name="submit" type="submit" class="btn btn-success" value="خرید" style="width: 66px"/>
</form>
<form action="insert.php" method="post">
<input name="track_code" type="text" class="tiny-size" value="<?php echo $track_code_rnd ; ?>" style="width: auto;height: auto" disabled />
</form>
<?php
$track_code = $_POST['track_code'];
?>
Set this one code in your form
<form action="insert.php" method="post">
<input name="track_code" type="text" class="tiny-size" value="<?php echo ($track_code_rnd)?$track_code_rnd:'';?>" style="width: auto;height: auto" disabled />
</form>
Remove disable tag from your input code. Disable controls wont be posted on server. for that you can make it readonly or make it hide
Try Putting the PHP code on the form. as given below.
<?php
if (isset($_POST['submit'])) {
$track_code = $_POST['track_code'];
}
?>
<form action="your_page.php" method="post">
<input name="track_code" type="text" class="tiny-size" value="<?php echo $track_code_rnd; ?>" style="width: auto;height: auto" readonly />
<input name="submit" type="submit" />
</form>
It will execute the whole file as PHP. The first time you open it, $_POST['submit'] won't be set because the form has not been sent. Once you click on the submit button, it will print the information.
Disabled controls never post on server. So make it readonly or hidden.

populate input field with PHP variable

I am creating a pizza ordering site with php. Now I want to echo the variable passed through the URL within the form. I know how to retrieve this data: <?php echo $_GET["numpizzas"]; ?>. But I don't know the proper way to add it to my html form field. any help is much appreciated
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="<?php echo "hi"; ?>" >
//Money field does not populate with number, I just see <?php echo $_GET[
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
<?php echo $_GET["numpizzas"]; ?>
I also tried storing the integer in a variable $howmanypizzas = $_GET["numpizzas"]; ?>but it still doesn't show up as the field value.
<?php echo $_GET["numpizzas"]; ?> does not only retrieve the data. echo also outputs it to the html response (the screen)
since you are allready passing your html with an ECHO, you can do:
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="'.$_GET["numpizzas"].'" >
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
Explanation: echo is a function that recieves a string and outputs it to html.
So, with the concatenation operator . you can inject the $_GET["numpizzas"] variable into your html, as a string, and pass it to the echo function, wich outputs it to the browser.
Another way to solve it is to only invoke PHP where you need to process your logic, just like #pavithra answer, wich also works.
You're already echoing and trying to echo inside of that. You need to concatenate your variable with the string that you are echoing, see PHP Strings:
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="' . $_GET["numpizzas"] . '">
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
You might also consider Heredoc syntax.
<input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
but i dont get why do you get this is as a get variable.hope this works
<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label> <input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>

Wordpress Custom Login Form which stores the data as well as emails the field values

If the above title looks confusing then here is the description....
I have a template page where I have placed the wordpress default registration form. Now what exactly I want is to add few extra fields on that form.
The wordpress registration will go on as it is. I mean the username and email and password gets stored on the database but along with that new fields or extra details like phone/address/age etc etc gets emailed to a specific email id.
<form method="post" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" class="wp-user-form">
<div class="username">
<label for="user_login"><?php _e('Username'); ?>: </label>
<input type="text" name="user_login" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" id="user_login" tabindex="101" />
</div>
<div class="password">
<label for="user_email"><?php _e('Your Email'); ?>: </label>
<input type="text" name="user_email" value="<?php echo esc_attr(stripslashes($user_email)); ?>" size="25" id="user_email" tabindex="102" />
</div>
<div class="login_fields">
<?php do_action('register_form'); ?>
<input type="submit" name="user-submit" value="<?php _e('Sign up!'); ?>" class="user-submit" tabindex="103" />
<?php $register = $_GET['register']; if($register == true) { echo '<p>Check your email for the password!</p>'; } ?>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?register=true" />
<input type="hidden" name="user-cookie" value="1" />
</div>
</form>
**Note:- The new fields aren't added here.
Is it possible? If yes, then how? Should I add a second form below this form which fires the email? Kindly please suggest an appropriate solution for this.

PHP Variable in HTML page

Can i add variables to an HTML page ?
Example:
I have this:
<form action="[FORUM_LOGIN_URL/]" enctype="application/x-www-form-urlencoded" id="loginForm" method="post" name="loginForm" onsubmit="return handleLogin();">
<div id="loginFormContainer">
<input id="loginField" name="login" type="hidden" value="login">
<input id="redirectField" name="redirect" type="hidden" value="[REDIRECT_URL/]">
<div>
<label for="userNameField">[LANG]userName[/LANG]:</label><br>
<input id="userNameField" maxlength="[USER_NAME_MAX_LENGTH/]" name="username" readonly="readonly" type="text" value="Liel">
</div>
<div>
<label for="passwordField">אין צורך בסיסמה:</label><br>
<input id="passwordField" name="password" readonly="readonly" type="password">
</div>
<div>
<label for="languageSelection">[LANG]language[/LANG]:</label><br>
<select id="languageSelection" name="lang" onchange="ajaxChat.switchLanguage(this.value);">
</select>
</div>
<div>
<input id="loginButton" name="submit" type="submit" value="[LANG]login[/LANG]">
</div>
</div>
</form>
I need a variable in the value:
<input type="text" readonly="readonly" value="!variable!" name="username" id="userNameField" maxlength="[USER_NAME_MAX_LENGTH/]"/></div>
I have also the lang.php and if i can add variable in lang line:
$lang['langexample'] = 'variable';
You can simply add php code in html with php tag. For example
<?php echo $something; ?>
Before that you need to make sure, you file have to save as ".php" extension.

Categories