Update SESSION variable - php

I have the following code in index.php:
<div class="done">
<b>Thank you<?php
echo $_SESSION['session_vname']." ";
echo $_SESSION['session_lname']."! </b><br><br>Email: ";
echo $_SESSION['session_email']."<br> Status: ";
echo $_SESSION['session_status']."<br>";
?>
</div>
and
<div class="form">
<form method="post" action="process.php">
<div class="element">
<label>First Name</label>
<input type="text" name="vname" class="text" />
</div>
<div class="element">
<label>Last Name</label>
<input type="text" name="lname" class="text" />
</div>
<div class="element">
<label>Email</label>
<input type="text" name="email" class="text" />
</div>
<div class="element">
<label>Status</label>
<input type="text" name="status" class="text" />
</div>
<div class="element">
<input type="submit" id="submit"/>
<div class="loading"></div>
</div>
</form>
</div>
I execute the form with an ajax submit so the index.php gets no refresh. When finished
gets a fadeOut and a fadeIn.
This works fine except for the following part:
When div class="form" is shown, it shows the session variable from the form before the last submit.
Example:
first input: session_vname = testa
first output $_SESSION['session_vname'] = nothing
second input: session_vname = overflow
second output $_SESSION['session_vname'] = testa
So finally the question:
How can I force the to update the session variables after form submitting also index.php is not reloaded?

PHP code is executed on server side, before the page is shown to the viewer. That means you need to update static elements with JavaScript if you don't want to refresh the whole page.

You cant update server side php sessions using javascript, you should be using cookies for that type of functionality

Related

Form not being posted when a file is attached (xamp localhost)

I have a strange issue where a form is posted correctly when the file input is empty, but nothing is posted when I add a file.
My html:
<form method="post" action="create_product.php" enctype="multipart/form-data">
<?php echo display_error(); ?>
<div class="input-groep">
Homepage
<label>Product naam</label>
<input type="text" name="product_naam" value="<?php echo $product_naam; ?>" autocomplete="off">
</div>
<div class="input-groep">
<label>Beschrijving</label>
<input type="text" name="beschrijving" value="<?php echo $beschrijving; ?>" autocomplete="off">
</div>
<div class="input-groep">
<label>Prijs</label>
<input type="text" name="prijs" value="<?php echo $prijs; ?>" autocomplete="off">
</div>
<div class="input-groep">
<label>Afbeelding</label>
<input type="file" name="afbeelding" accept=".jpg,.jpeg,.png" autocomplete="off">
</div>
<div class="input-groep">
<button type="submit" class="homeknopdonker" name="register_product"> + Maak product aan</button>
Terug naar producten
</div>
</form>
When I submit the form without any values, I can see in my network tab the following:
product_naam:
beschrijving:
prijs:
afbeelding: (binary)
Above data is inside Form Data
However if I attach a file (small image) to the afbeelding input (file) the Form Data dissapears entirely from my network tab when I post it. The paramaters are nowhere to be seen and I get no errors when I turn on error_reporting.
What could be causing this?

AMP Project doesn´t excecute the PHP code as Form or even as echo

I have a website AMP project and I wanted to use PHP for forms.
On the webpage form which has a post method returns no action in the backend code.
So I think that the data inn´t transfered.
edit on answer:
My form code is:
<form method="POST" class="sit-form" action-xhr="ampsend.php" data-form-title="contactForm">
<div class="sit-row">
<div submit-success="" class="sit-col-lg-12 sit-col-md-12 sit-col-sm-12">
<template data-form-alert="" type="amp-mustache">success!
</template>
</div>
<div submit-error="" class="sit-col-lg-12 sit-col-md-12 sit-col-sm-12">
<template data-form-alert="" type="amp-mustache">failed! {{error}}</template>
</div>
</div>
<div class="dragArea sit-row">
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="firstnameHidden" value="Firstname"
id="firstnameHidden"
data-form-field="">
<input type="text" name="firstname" placeholder="Firstname" data-form-field="Name"
required="required" class="field-input display-4" value=""
id="firstname">
</div>
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="lastnameHidden" value="Lastname"
id="lastnameHidden"
data-form-field="">
<input type="text" name="lastname" placeholder="Lastname" data-form-field="Name"
class="field-input display-4" required="required" value=""
id="lastname">
</div>
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="emailHidden" value="Email" id="emailHidden"
data-form-field="">
<input type="email" name="email" placeholder="Email" data-form-field="Email"
required="required" class="field-input display-4" value=""
id="email">
</div>
<div class="sit-col-md-12 sit-col-sm-12 field">
<input type="hidden" name="messageHidden" value="Message"
id="messageHidden"
data-form-field="">
<textarea name="message" placeholder="Message" data-form-field="Message"
class="field-input display-4" required="required" value=""
id="message"></textarea>
</div>
<div data-for=""
class="sit-col-md-12 sit-section-btn sit-pt-4 align-center sit-col-sm-12 field">
<button type="submit" class="btn btn-primary display-4">
send
</button>
</div>
</div>
</form>
The file_put_contents of the POST by clicking on submit is:
a:8:{
s:15:"firstnameHidden";
s:9:"Firstname";
s:9:"firstname";
s:8:"TestName";
s:14:"lastnameHidden";
s:8:"Lastname";
s:8:"lastname";
s:12:"TestLastname";
s:11:"emailHidden";
s:5:"Email";
s:5:"email";
s:13:"test#test.com";
s:13:"messageHidden";
s:7:"Message";
s:7:"message";
s:11:"testMessage";}
So does it work correctly? because as return I get the "failed!" - message from the AMP template.
I think you cannot execute PHP code on the displaying document. PHP is serving the data to show through an POST on the displaying web document as described in this post.
My solution to check if PHP is running ,and check what data was posted via form - was write everything to text file.
$serializedData = serialize($_POST);
file_put_contents('your_file_name.txt', $serializedData);
Open file and see what data was pass. If you want display message after form was submitted and pass some date like name or email. You need to use amp-mustache. And and return JSON form PHP to pass it back to your AMP page.
For example in your PHP file:
header('Content-Type: application/json');
echo json_encode(array( 'name' => $_POST['name]' ));
Of course don't forget to put appropriate CORS headers in PHP or you .htaccess file.

form is sending variables by get method

i got a form.
<form id="site-contact-form">
<div>
<div class="wrapper"><span>Ձեր անունը:</span>
<div class="bg">
<div>
<input type="text" class="input" name="contactname" id="contactname" />
</div>
</div>
</div>
<div class="wrapper"><span>Ձեր E-mail-ը:</span>
<div class="bg">
<div>
<input type="text" class="input" name="email" id="email" />
</div>
</div>
</div>
<div class="textarea_box"><span>Տեկստ:</span>
<div class="bg">
<div>
<textarea cols="1" rows="1" name="message" id="message"></textarea>
</div>
</div>
</div>
<div style="clear:both;"></div>
<button id="sub" name="submit">ուղարկել</button>
</div>
</form>
As you see i got no action on it, no method. But when im clicking on button, its refreshing the page (like when it have action) and adding to URL ?contactname=&email=&message=&submit= ... I never met this problem before, why it is sending variables? I dont have any php on page yet...
Beacause the default method is GET and the URL is the same of the page. Use:
<form method="POST" action="/reactor">
<!-- .... -->
</form>
Actually, the action property is mandatory.
Please correct the code with:
<form id="site-contact-form" method="post">
And the submitted values will not be displayed on the URL.
If you do not specify the form method, it will "get" by default.
This is happening here.
Change the type of button as button
<button id="sub" name="submit" type="button">ուղարկել</button>

clearing the textbox input on button click

i do have multiple text boxes called first name,last name,phone no and so on.. and i want to clear/refresh the input typed in it on button click called as refresh or clear.the problem is that the input in these text boxes are not getting refreshed because the data in it is coming from other page and it is getting refreshed when we type some thing in it
my code is as follows:
<div class="control-group">
<input value="<?php echo $model['pros']['oppid'] ?>" name="oppid" type="hidden" id="prospectid" />
<label class="control-label">First Name </label>
<div class="controls">
<input type="text" name="f_name" pattern="[-a-zA-Z]+" required="" title="First Name is required" value="<?php echo $model['pros']["f_name"]; ?>" />
</div>
</div>
<div class="control-group">
<label class="control-label">Last Name </label>
<div class="controls">
<input type="text" name="l_name" pattern="[-a-zA-Z]+" required="" title="Last Name is required" value="<?php echo $model['pros']["l_name"];?>" />
</div>
</div>
<div class="control-group">
<label class="control-label">Primary phone </label>
<div class="controls">
<input type="tel" required="" pattern="[-0-9]+" title="Please Enter correct phone" name="pri_phone" value="<?php echo $model['pros']["pri_phone"];?>" />
</div>
</div>
and the button as
<button class="btn">New</button>
please suggest me on this...
use the following code:
$("#elementid").live('click',function(){
$(this).parents('form').find('input[type="text"]').val('');
});
Assuming that your input elements are inside of a form, and so too is your button, you can do this using jQuery:
$('button.btn').click(function(e) {
e.preventDefault();
$(this).parents('form').find('input[type="text"], input[type="tel"]').val('');
});
you can write code in jquery as:
$('#new').live('click',function(){
$('#fname').val('');
$('#lname').val('');
$('#tel').val('');
});
If your button is not within the form you can use the following
$('button.btn').click(function() {
$('.control-group input:not(:submit)').val('');
});
This will reset any type of input within the control-group divs except for submit inputs
If your reset button is within the form then BenM's answer is what you want.
$(".clearform").live('click',function(){
$(this).parents('form').find('input[type="text"], input[type="tel"],input[type="time"],input[type="email"],input[type="number"],input[type="date"],select').val('');
});

Using "action" in Twitter Boostrap classes

I'm a newbie programmer trying to utilize Twitter Bootstrap to build out a concept. I'm using PHP and assigning actions within HTML tags to POST data and essentially take a user through the navigation. This has been pretty straightforward when using forms: i.e. here is a snippet that does work. For example, for this snippet of HTML:
<form action="?viewnotes" method="post">
<input type="hidden" name="id" value="<?php htmlout($note['id']); ?>">
</form>
It successfully triggers the following if statement in my index.php:
if (isset($_GET['viewnotes']))
However, I'm trying to do the same thing in my registration page, using a Twitter Bootstrap class for buttons. Here is the HTML:
<a class="btn btn-primary btn-large" action="?register">Sign Up Free!»</a></p>
The PHP code is:
if (isset($_GET['register']))
{
include 'register.html.php';
}
Clicking on the Sign Up button is not invoking the PHP code. If I hard code the URL it works, but then I have a similar issue on the register.html.php page. HTML on the register page has:
<form class="form-horizontal" action="?storeuser" method="post">
<fieldset>
<legend>It's quick and easy...</legend>
<div class="control-group">
<label class="control-label">First Name</label>
<div class="controls">
<input type="text" name="fname" class="input-xlarge" id="fname">
</div>
</div>
<div class="control-group">
<label class="control-label" name="lname">Last Name</label>
<div class="controls">
<input type="text" name="lname" class="input-xlarge" id="lname">
</div>
</div>
<div class="control-group">
<label class="control-label" name="email">Email Address</label>
<div class="controls">
<input type="text" name="email" class="input-xlarge" id="email">
</div>
</div>
<div class="control-group">
<label class="control-label" name="password">Password</label>
<div class="controls">
<input type="password" name="password" class="input-xlarge" id="password">
</div>
</div>
</fieldset>
<button type="submit" name="action" class="btn btn-primary btn-large">Complete Registration</button>
</form>
However, when clicking on the button, the index file does not trigger the following, which would store the fields into the DB.
if (isset($_GET['storeuser']))
If I hardcode the URL to register.html.php, then the resulting URL looks like localhost/register.html.php?storeuser instead of localhost/?storeuser. I'm not sure if that's affecting the behavior here.
Thank you for the help!
I think you're approaching this the wrong way, and it's not Twitter Bootstrap's fault.
Usually, you'd use POST, not GET, to handle user registrations. Your form would look like this:
<form action="register.php" method="post">
<!-- your form -->
<fieldset>
<input type="submit" name="register" value="Register" class="btn btn-primary" />
</fieldset>
</form>
You can then build register.php as follows:
<?php
if (isset($_POST['register'])) {
// handle user registration
}
// display form
?>
This will display the form when the user visits register.php, but will try and process the user registration first if the form's been POSTed.
try passing a value with your GET variable
<form action="?viewnotes=1" method="post">

Categories