Serialize array & put results into hidden text field? - php

I'm currently encountering a problem which is baffling me. There will be a lack of PHP mixed with HTML due to using a MVC Framework.
What i've got:
Form A on Page 1 with a submit button which directs user input from Form A to page 2 with a chunk of text for the user to read before continuing, a hidden text field which will contain a serialized array from Form A and a submit button to continue to the validation of user input.
My forms direct to the correct pages as required, the post array can be seralized providing I do not put it into a text field. The HTML from form A follows:
<form action="/RegisterInformation" method="POST">
<div class="row">
<div class="large-12 columns">
<input type="text" name="Username" placeholder="Username, ex: JohnDoe ">
</div>
</div>
<div class="row">
<div class="large-4 columns">
<input type="password" name="Password" placeholder="Password">
</div>
<div class="large-4 columns">
<input type="password" name="cpassword" placeholder="Confirm Password">
</div>
<div class="large-4 columns">
<input type="text" name="email" placeholder="Email, ex: JohnDoe#provider.com">
</div>
</div>
<div class="row">
<div class="large-12 columns">
<textarea name="Referral" placeholder="Where Did You Hear About Us?"></textarea>
</div>
</div>
<div class="row">
<div class="large-4 columns">
<input type="submit" name="submit" value="Continue" class="button">
</div>
</div>
</form>
The redirect to page 2:
<div class="row">
<div class=" large-12 columns">
<p>
<?php echo $data['PreregisterInformation']; ?>
</p>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<form action="/Continue" method="POST">
<input type="text" name="SubmitInfo" value="<?php echo serialize($_POST); ?>">
<input type="submit" name="submit" value="Continue" class="button">
</form>
</div>
</div>
With the current example, i've got the text field set to being visible, so I can see what's going on.. But this is where it gets strange.
What i'm getting is two different types of results.
The first, is that when I echo the serialized array outside a input field. I get the desired results:
a:6:{s:8:"Username";s:1:"s";s:8:"Password";s:1:"4";s:9:"cpassword";s:1:"4";s:5:"email";s:3:"asd";s:8:"Referral";s:3:"asd";s:6:"submit";s:8:"Continue";}
The second is when I use the following:
<input type="text" name="SubmitInfo" value="<?php echo serialize($_POST); ?>">
Which passes an invalid serialized array to page 3 breaking the whole process.
I assume it's not a HTML related error, but the new line break in the serialized array definition a:6. I have made an attempt to rectify this problem by removing the new line break with str_replace:
$Ser = serialize($_POST);
$Ser = nl2br($Ser);
$Ser = str_replace("<br>","00",$Ser);
echo $Ser;
which has proven not to work.

This is not a direct solution of your problem, but have you tried json_encode or avoiding the hidden field by storing the date into $_SESSION?

Related

files not being posted into $_FILES super global array

I am having trouble with a file upload portion of a project. I have created the form with the inputs but when i try to "upload" the file it does not get posted into the $_FILES array. Code posted below. I have declared an enctype within the from tag but the file still does not get posted into the array it only shows Array()
here is my form code:
<?php
require("includes/application_top.php");
$pageTitle = "Add A Product";
require("includes/header.php");
print_r($_FILES);
?>
<div class="container-fluid py-4">
<div class="row ">
<div class="col">
<form method="POST" action="s.php" enctype="multipart/form-data">
<!-- Product Name input -->
<div class="form-outline">
<input type="text" id="ProductName" class="form-control" />
<label class="form-label" for="ProductName"> Product Name</label>
</div>
</div>
<div class="col">
<!-- Product Description input -->
<div class="form-outline">
<input type="text" id="ProductDesc" class="form-control" />
<label class="form-label" for="ProductDesc">Product Description</label>
</div>
</div>
</div>
<hr />
<div class="row">
<div class="col">
<!-- Product Price input -->
<div class="form-outline">
<input type="number" min="0.00" max="10000.00" step="0.01" id="ProductPrice" class="form-control" />
<label class="form-label" for="ProductPrice">Product Price </label>
</div>
</div>
<div class="col">
<!-- Img Upload input -->
<div class="form-outline input-group mb-3 ">
<input type="file" id="ImageUpload" class="form-control" />
<input type="submit" value="Upload" />
</div>
</div>
</div>
</form>
</div>
<?php
require("includes/footer.php")
?>
Any form field must have a name attribute in order to populate PHP's $_GET , $_POST or $_FILES superglobals after submitting the form.
$_GET and $_POST depend on the form method, and $_FILES is specific to file uploads (HTTP POST method only).
Your file input should then look like this:
<input type="file" id="ImageUpload" class="form-control" name="image_upload" />
Given the image_upload name I wrote, you should be able to get the file informations using $_FILES['image_upload].
Read about file uploads in the documentation: Handling file uploads.
You have to provide a name attribute to your fields, both $_POST and $_FILES work on the name attribute, not the ID. id is mostly used when you do something with the field using javascript, like when submitting the form with an ajax request.
In your case, you would need names:
<input type="file" id="ImageUpload" class="form-control" />
would become
<input type="file" name="ImageUpload" class="form-control" />
The same goes for your other input fields, you need a name attribute before you will see then in $_POST.

How can I use multiple input type file in one html form?

Recently I need to use multiple input type file in one form. But when I post the form its returning null. Like I have nothing on $_POST. I have also add the enctype="multipart/form-data". I am stacked for last 2 days. Googled for many times. But still didn't get any solution. Please help me to out of this.
<form enctype="multipart/form-data" action="/admin/process_post/savepost" method="POST">
<div class="uk-grid uk-grid-small" data-uk-grid-margin="">
<div class="uk-width-medium-7-10">
<div class="md-card">
<div class="md-card-content">
<h3 class="heading_a uk-margin-medium-bottom">Add New Canvas</h3>
<div class="uk-grid" data-uk-grid-margin>
<div class="uk-width-medium-1-1">
<div class="uk-form-row">
<label>Post Title</label>
<input type="text" name="post_title" id="post_title" class="md-input" />
</div>
<div class="uk-form-row">
<label>Post Description</label>
<textarea id="post_desc" name="post_desc" cols="30" rows="20"></textarea>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="uk-width-medium-3-10">
<div class="md-card">
<div class="md-card-content">
<div class="uk-form-row">
<p>
<input type="checkbox" name="post_status" id="post_status" checked data-md-icheck />
<label for="post_status" class="inline-label">Post Status</label>
</p>
</div>
</div>
</div>
<div class="md-card">
<div class="md-card-content">
<div class="uk-form-row">
<label for="post_attachment" class="inline-label">Post Featured Image</label>
<input type="file" id="post_attachment" name="post_attachment">
</div>
</div>
</div>
<div class="md-card">
<div class="md-card-content">
<div class="uk-form-row">
<label for="result_img" class="inline-label">Post result Image</label>
<input type="file" id="result_img" name="result_img" multiple="multiple">
</div>
</div>
</div>
<div class="md-card">
<div class="md-card-content">
<div class="uk-form-row">
<button type="submit" class="uk-form-file md-btn md-btn-primary">Save Post</button>
</div>
</div>
</div>
</div>
</div>
</form>
I know this is a silly situation for a developer when they face problem like this.
Files can be gotten through $_FILES
When doing multiple uploads at same name, name it as array appending [] at end of name example: ( name="images[]" )
If upload continues to appear as empty, and you said in comment that with one file it's doing right, try to change at php.ini post_max_size and upload_max_filesize to a larger amount. Also check max_file_uploads - the number of files allowed for uploads per single request.
Your problem should be your form action, try to put an "/" on the end of it and see if it works:
<form enctype="multipart/form-data" action="/admin/process_post/savepost/" method="POST">
You need to change your file input name to an array
<input type="file" id="result_img" name="result_img[]">
Also try adding multiple
<input type="file" id="result_img" name="result_img[]" multiple>
To check for files you must use $_FILES instead of $_POST

php form submit in div fails

after many days of trying to get a previously working php form converted to submitting the variables inside a new div I realized that I'm missing something. Other posts show javascript, but Iv'e never used that before and don't understand the need. The new page draws correctly, but the php variables are not being received on the destination page.
HTML for the submit,
<form action="entrance2.php">
<div class="medium-12 columns m-b20"><h4 class="heading">Existing users log-in here :-</h4></div>
</div>
<div class="row">
<div class="user medium-12 columns text-center m-b15"><img src="images/user-img.png" alt=""/></div>
</div>
<div class="row">
<div class="medium-10 columns medium-offset-1"><label for="User Name"></label>
<input id="OwnerEmaili" type="text" placeholder="User Name" name="UserName"></div>
</div>
<div class="row">
<div class="medium-10 columns medium-offset-1"><label for="Password"></label>
<input id="OwnerPasswordi" type="password" placeholder="Password" name="Password"></div>
</div>
<div class="row">
<div class="medium-12 columns text-center"><button class="grd-button">Log In</button></div>
<input type="submit" id="save" name="save" value = "Submit"/>//simple submit for testing
<div class="grd-button1" onClick="document.forms['submit-form'].submit();"></div>
</form></div>
</div>
</div>
Receiving page,
<?php
$p_OwnerEmaili=$_POST["OwnerEmaili"];
$p_OwnerPasswordi=$_POST["OwnerPasswordi"];
echo "$p_OwnerEmaili;$p_OwnerPasswordi";
Only shows the ;.
Is javascript required to submit from inside a div?
You're accessing the wrong items.
You'll need to set your forms input name attributes to this if you want to access them the way you currently have in your php script:
<input id="OwnerEmaili" type="text" placeholder="User Name" name="OwnerEmaili">
And
<input id="OwnerPasswordi" type="password" placeholder="Password" name="OwnerPasswordi">
That will allow you to access them as you do in your PHP script.
You can always check what values have been sent to your php script by using var_dump() or print_r().
<?php print_r($_POST); ?>
Would've shown you that you had UserName & Password set instead of what you wanted.
As Ghost pointed out in the comments, your form will always send user input via GET if you dont specify a method in it. So set this in your form tag:
<form action="entrance2.php" method="post">

HTML/PHP Form not working

I have a HTML Form that uses partial PHP to grab the value, the form is basically like an edit account details form.
The Problem
I cannot work out why the form is not working and when using notepad++ to edit my code if I click on the it shows the start to be a DIV which just confuses the matter even more... When submitting the form it takes you back to the form page with no message so I am lost for a reason..
Form Page
<form method="POST" action="dev.php">
<!-- Row -->
<div class="row-fluid">
<!-- Column -->
<div class="span6">
<!-- Group -->
<div class="control-group">
<label class="control-label" for="fname">First name</label>
<div class="controls">
<input type="text" name="fname" id="fname" value="<?php echo $user_fname; ?>" class="span10" />
<span style="margin: 0;" class="btn-action single glyphicons circle_question_mark" data-toggle="tooltip" data-placement="top" data-original-title="First name is mandatory"><i></i></span>
</div>
</div>
<!-- // Group END -->
<!-- Group -->
<div class="control-group">
<label class="control-label" for="lname">Last name</label>
<div class="controls">
<input type="text" name="lname" id="lname" value="<?php echo $user_sname; ?>" class="span10" />
<span style="margin: 0;" class="btn-action single glyphicons circle_question_mark" data-toggle="tooltip" data-placement="top" data-original-title="Last name is mandatory"><i></i></span>
</div>
</div>
<!-- // Group END -->
</div>
<!-- // Column END -->
<!-- Column -->
<div class="span6">
<!-- Group -->
<div class="control-group">
<label class="control-label" for="email">Email Address</label>
<div class="controls">
<input type="text" name="email" id="email" value="<?php echo $user_email; ?>" class="span10" />
<span style="margin: 0;" class="btn-action single glyphicons circle_question_mark" data-toggle="tooltip" data-placement="top" data-original-title="First name is mandatory"><i></i></span>
</div>
</div>
<!-- // Group END -->
<!-- Group -->
<div class="control-group">
<label class="control-label" for="phonenumber" >Phone Number:</label>
<div class="controls">
<input type="text" name="phonenumber" id="phonenumber" value="<?php echo $user_number; ?>" class="span10" />
</div>
</div>
<!-- // Group END -->
</div>
<!-- // Column END -->
</div>
<!-- // Row END -->
<div class="separator line bottom"></div>
<!-- Group -->
<div class="control-group row-fluid">
<label class="control-label" for="bio">About me</label>
<div class="controls">
<textarea id="bio" name="bio" class="span12" rows="5"><?php echo $user_bio;?></textarea>
</div>
</div>
<!-- Form actions -->
<div class="form-actions" style="margin: 0;">
<button type="submit" id="accountdetails" name="accountdetails" class="btn btn-icon btn-primary glyphicons circle_ok"><i></i>Save changes</button>
</div>
</div>
</form>
<!-- // Form actions END -->
dev.php
if (isset($_POST['accountdetails'])) {
if (isset($_POST['fname']) || isset($_POST['lname']) || isset($_POST['email']) || isset($_POST['phonenumber']) || isset($_POST['bio'])) {
die ("HERE");
};
};
I probably need to drink more Coffee but I cannot for the life of me work out why it is not working.
Any help would be appreciated!
Thanks in advance.
EDIT
I put the name's in and this did not help, now the URL of the page shows this:
update.php?fname=Aaron&lname=Hatton&email=me%40aaronhatton.co.uk&phonenumber=0123456789&bio=+18+%7C+London+%7C+Taken&accountdetails=
any ideas?
One: do what Fred said (name attributes on your input tags).
Two: You're missing the </form> tag at the end.
Your form page seems correct, however, since the form updates the user data, look what the dev.php code is doing:
isset() function returns true if the value is set and you manual set the fields so it will evaluate to true.
and in your if statement you are ORing all the conditions so as soon as it finds 1 true condition, it will go into the if statement body and execute die which will do nothing.
so if you want to test, instead of using die, try echo "here" to see if a message is printed.
So I found out the form works perfect and there was some AJAX being used by another coder, removed and surprise surprise it works perfectly!
Damn co-workers!
Thanks to all that helped!

PHP Multiple GET methods in one your

I'm making a tool to pull eve API data and I use a GET form to take the data and get access to the key ID and code to access the data. This then pulls the first character ID on the key and grabs all that character data.
for example the url is:
whatever.com/APIChecker/?keyID=123456&code=ABCDEFGHIJKL
I pull these variables out via the session variables that they create.
What I'm now trying to do is now have a for loop which puts up buttons for each character on the key so you can flick between characters, I'm trying and have tried get and post methods hidden in the button but can't seem to get my desired result. It seems to not be taking out the variable that i'm trying to pass and adding it into the $_SESSION array or the $_POST or the $_GET - i've tried print_r on all these variables but I can't get any output.
Here is my primary form thats on my main page:
<form method="get" action="APIChecker/" >
<legend> Submit an API Key</legend>
<div class="control-group">
<label class="control-label">Key ID:</label>
<div class="controls">
<input type="text"
class="input-small"
name="keyID""/>
</div>
</div>
<div class="control-group">
<label class="control-label">Verification Code:</label>
<div class="controls">
<input type="text"
class="input-xxlarge"
name="vCode"
/>
<p class="help-block"></p>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Submit API</button>
</div>
</div>
What I'm trying to do now is get another session variable from another page - which updates a variable and refreshes the page with the keyID and code unaltered.
As in:
whatever.com/APIChecker/?keyID=123456&code=ABCDEFGHIJKL **&charID=654321**
so $_SESSION['keyID'] = 123456
& $_SESSION['code'] = ABCDEFGHIJKL
& $_SESSION['charID'] = 654321
what I've tried is:
<form method="get" action="" >
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">
<input type="hidden"
name="charID"
value="$charID"
/>
</button>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Submit API</button>
</div>
</div>
</form>
You can't output PHP values directly in HTML like this: value="$charID"
Try this instead:
value="<?php echo $charID; ?>"

Categories