My point is after sending form show session data.
form.html:
<form action="sendForm.php" name="f" method="post" onsubmit="return sendForm()" >
<div class="form">
<label for="imie">Name</label>
<input type="text" id="nam" name="nam"/><br/>
<label for="nazwisko">Surname</label>
<input type="text" id="surname" name="surname"/><br/>
</div>
<input type="submit" name="send" id="send" value="send form"/><br/><br/>
</form>
sendForm.php:
<?php
session_start();
$_SESSION['nam'] = $_POST['nam'];
$_SESSION['surname'] = $_POST['surname'];
header("Location: view-struct.php");
?>
view-struct.php:
<span class="label">Name</span><?php echo $_SESSION['nam']; ?><br/>
<span class="label">Surname</span><?php echo $_SESSION['surname']; ?><br/>
[EDIT] Question is closed, forgot to add for session_start() in view-struct.php
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.
in my code when i click on submit 3 btn it echos back empty $email though post is created in db the title is always empty, however if i change if(isset($_POST['submit3'])) to if(isset($_POST['submit1'])), it doesn't enter the details into db or echo back anything
<script type="text/javascript">
$( document ).ready(function() {
$("#form1").show();
$("#form2").hide();
$("#form3").hide();
$('button[name="submit1"]').click(function(e) {
$("#form2").show();
$("#form1").hide();
$("#form3").hide();
e.preventDefault();
});
$('button[name="submit2"]').click(function(e) {
$("#form3").show();
$("#form1").hide();
$("#form2").hide();
e.preventDefault();
});
});
</script>
<body>
<form id="form1" method="post" >
<div>
<input type="text" id="inputemail" name="email" placeholder="Email" height="34px" >
</div>
<div >
<button id="submit1" type="submit" name="submit1" ><strong>Start</strong></button>
</div>
</form>
<form id="form2" method="post" style="display:none">
<div>
<input type="text" id="inputpwd" name="pwd" placeholder="Password" height="34px" required>
</div>
<div >
<button id="submit2" type="submit" name="submit2" ><strong>Next</strong></button>
</div>
</form>
<form id="form3" method="post" style="display:none">
<div required>
<input type="checkbox" value="0" name="book_type[]"> Romance</input><br>
<input type="checkbox" value="1" name="book_type[]"> Action</input><br>
<input type="checkbox" value="2" name="book_type[]"> suspense</input><br>
<input type="checkbox" value="3" name="book_type[]"> thriller</input><br>
</div>
<div >
<button id="submit3" type="submit" name="submit3" onclick="submitform();"><strong>Next</strong></button>
</div>
</form>
</body>
<?php
$email=$_POST['email'];
$pwd=$_POST['pwd'];
$bookchk=array();
$todaydate = new DateTime();
$todaydate =$todaydate->format('Y-m-d H-i-s');
if(isset($_POST['submit3']))
{
echo $email;
$formarg= array('post_type'=>'details',
'post_title'=>$email,
'post_author'=>'1',
'post_status'=>'publish',
'post_date'=>$todaydate );
$wpdb->insert('wp_posts',$formarg);
$bookpostid=$wpdb->insert_id;
foreach($_POST['book_type'] as $selectedplan)
{
$bookchk[]=$selectedplan;
}
update_field('field_561b9999ebb56',$bookchk[0],$contestpostid);
update_field('pwd',$pwd,$bookpostid);
}
?>
Try it:
<button id="submit3" type="submit" name="submit3" value="Next" onclick="submitform();"><strong>Next</strong></button>
<?php
session_start();
if(isset($_POST['email'])){$_SESSION["email"] = $_POST['email'];}
if(isset($_POST['pwd'])){$_SESSION["pwd"] = $_POST['pwd'];}
if(isset($_POST['book_type'])){$_SESSION["book_type"] = $_POST['book_type'];}
if(isset($_SESSION["email"]) && isset($_SESSION["pwd"]) && isset($_SESSION["book_type"])){
echo "email:".$_SESSION["email"]."<br>";
echo "pwd:" .$_SESSION["pwd"]."<br>";
echo("options:");
for($i=0;$i<sizeof($_SESSION["book_type"]);$i++){
echo $_SESSION["book_type"][$i]."|";
}
session_destroy();
$_SESSION["email"] = "";
$_SESSION["pwd"] = "";
$_SESSION["book_type"] = "";
}
$todaydate = new DateTime();
$todaydate =$todaydate->format('Y-m-d H-i-s');
?>
<body>
<?php if(empty($_SESSION["email"])):?>
<form id="form1" method="post">
<div>
<input type="text" id="email" name="email" placeholder="Email" height="34px" >
</div>
<div >
<input id="submit1" type="submit" name="submit1" value = "Start">
</div>
</form>
<?php endif;?>
<?php if(isset($_SESSION["email"]) && empty($_SESSION["pwd"]) ):?>
<form id="form2" method="post">
<div>
<input type="text" id="pwd" name="pwd" placeholder="Password" height="34px" required>
</div>
<div >
<input id="submit2" type="submit" name="submit2" value = "Next">
</div>
</form>
<?php endif;?>
<?php if(isset($_SESSION["email"]) && isset($_SESSION["pwd"]) && empty($_SESSION["book_type"])):?>
<form id="form3" method="post">
<div required>
<input type="checkbox" value="0" name="book_type[]"> Romance</input><br>
<input type="checkbox" value="1" name="book_type[]"> Action</input><br>
<input type="checkbox" value="2" name="book_type[]"> suspense</input><br>
<input type="checkbox" value="3" name="book_type[]"> thriller</input><br>
</div>
<div >
<button id="submit3" type="submit" name="submit3" onclick="submitform();"><strong>Next</strong></button>
</div>
</form>
<?php endif;?>
</body>
You are getting back empty $email when submitting through button
id="submit3" because an input field with attribute name="email"
doesn't exists within your third form <form id="form3" ...> </form>
You are receving nothing when submitting through button
id="submit1" because e.preventDefault(); method prevents form from
submitting when you click submit button.
To proceed submitting of your first and second form remove e.preventDefault(); from submit button click handlers
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.
At the top I have a session start and the session ID can be passed across all the webpages because the ID is the same on all, this also includes a session variable called 'username' with the value of Guest.
I want to change this variable to the one entered in the form.
This is my first post so sorry for any mistakes.
<form class="form1" method="post" action="./" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
<?
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
}
?>
Thanks
You need to take out ./ is in the action="" quotes like below (this is because you are using the same file to process the form)... and always start your php opening with <?php
<form class="form1" method="post" action="" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
}
?>
And if you want to test it try something like this:
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
// Use the following code to print out the variables.
echo 'Session: '.$_SESSION['username'];
echo '<br>';
echo 'POST: '.$_POST['username'];
}
?>
Tested the code after making the changes, and it works fine... look:
Update answer based of requests in the comments
<form class="form1" method="post" action="./" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
PUT THIS IN YOUR index.php
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
// Use the following code to print out the variables.
echo 'Session: '.$_SESSION['username'];
echo '<br>';
echo 'POST: '.$_POST['username'];
}
?>
The script for handling the action of my form redirects to the form page if the values are not in proper format. I want to fill the textfields and textarea with the faulty data the user entered on redirect to the form page. I have written the following script which redirects the page on wrong value submission, but does not fill the fields thereafter.
script on form page:
<?php
if(session_id('stat')=="true")
{
$isbn=$_SESSION['var1'] ;
$name=$_SESSION['var2'] ;
$author=$_SESSION['var3'] ;
$publisher=$_SESSION['var4'];
$price=$_SESSION['var5'];
$descrip=$_SESSION['var6'];
$status=$_SESSION['stat'];
}
else
{
$isbn="";
$name="";
$author="";
$publisher="";
$price="";
$descrip="";
$status=false;
}
?>
The html part of the form:
<form action="scripts/addscript.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">
<label for="isbn">ISBN</label>
<input type="text" name="isbn" id="isbn" value="<?php $isbn ?>"/>
<p>
<label for="name">Name</label>
<input type="text" name="name" id="name" value="<?php echo $name; ?>"/>
</p>
<p>
<label for="author">Author</label>
<input type="text" name="author" id="author" value="<?php echo $author; ?>"/>
</p>
<p>
<label for="publisher">Publisher</label>
<input type="text" name="publisher" id="publisher" value="<?php echo $publisher; ?>"/>
</p>
<p>
<label for="price">Price</label>
<input type="text" name="price" id="price" value="<?php echo $price;?>"/>
</p>
<p>
<label for="description">Description</label>
<textarea name="description" id="description" cols="45" rows="5"><?php echo $descrip; ?></textarea>
</p>
<p>
<label for="img">Select an image for the book:
<input type="file" name="img" id="img" />
</label>
<input type="submit" name="submit" id="submit" value="Submit"/>
</p>
</form>
The redirecting script on addscript.php to which the form values are submitted:
<?php
// Get values from form
$isbn=$_POST['isbn'];
$name=$_POST['name'];
$author=$_POST['author'];
$publisher=$_POST['publisher'];
$price=$_POST['price'];
$descrip=$_POST['description'];
$_SESSION['var1'] = $isbn;
$_SESSION['var2'] = $name;
$_SESSION['var3'] = $author;
$_SESSION['var4'] = $publisher;
$_SESSION['var5'] = $price;
$_SESSION['var6'] = $descrip;
if(strlen($isbn)==0||strlen($name)==0||strlen($author)==0||strlen($publisher)==0||strlen($price)==0)
{
$_SESSION['stat']="true";
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
Please telll me where is the problem and how can I solve the issue?
Thanks in advance.
session_start() must be called at the top of your PHP script to use the $_SESSION variable.