generate and save sequential number on POST - php

I have a form that uses Post to send info to a PHP page.
The PHP page;
Takes the form info.
Rearranges it.
Formats it.
Then turns it into a code string that's ultimately sent to another
page and performs some tasks using that info.
Form Code:
<form method="post" action="_php/buildMyPNR.php" name="GuestInfo"
onSubmit="return validateFormMethod1();">
<input type="text" name="AccountNumber" id="BID"
onkeypress="return isNumberKey(event)" size ="16"/>
<input type="hidden" name="requestID" id="RID" value="" />
..... bunch of other fields
<input type="submit" name="loadURL" id="submit" value="Submit"
onsubmit="return ValidateFields();" />
</form>
What I need to do is;
Generate a sequential number each time the page is used.
Pass that number with the post request (Sort of like a serial number
for the request), to another form say _php/buildMyPNR.php.
When _php/buildMyPNR.php loads, save the values of
AccountNumber and requestID to a database or similar.
Ultimately it would move on to _php/usageSummary.php, where the user can click
a button to return a list of AccountNumbers paired with the
requestID that was generated for the request.
Possibly download the list in an excel spreadsheet format, but it is
not absolutely necessary.
What would be the best way to accomplish this?
Mainly I need help with generating the requestID. I need them to be sequential so I assume that I would need to start with a number in my database like 00001, query that number from my form, add 1 to the returned value, then save the new value along with the AccountNumber when submit is pressed.
I have no experience with databases and any guidance would be greatly appreciated.

Ideally we'd see some schema and examples of code you've already tried...
However, you can use an Auto Increment field in your Database Table, which will accomplish the ID incrementing for you.
If you're using mysqli for your mysql database interaction (as you perhaps should be), then you can then retrieve this autonumber using;
$PassedRequestID = $mysqli->insert_id
Then pass this to your next page.
You will then need to query your database with something like (psuedo code);
SELECT Accounts.*, Requests.*
INNER JOIN Requests ON Requests.AccountID = Accounts.AccountID
WHERE Requests.RequestID = $PassedRequestID
For exporting to Excel, if you search for PHP export CSV using Google, there'll be plenty of examples there to choose from. However, as an example;
http://code.stephenmorley.org/php/creating-downloadable-csv-files/

Related

How to add date and time when html form is submitted in PHP pure scripting?

I am making a basic post-box management system in PHP. the postman can add 2 inputs which are 'delivered by' and 'item number'. and a button for the postman to click to submit the inputs.
Now, when the submit button is clicked, I want PHP to record the date and time of when the inputs are being submitted (so that later on I can set a mechanism to track the item duration and see how long an item has been in the box-but this is not part of the question). How do I do that?
sorry I have just started learning PHP so my current code is still very basic, and I haven't start with the Box-function.php page yet:
The HTML page:
<form method="post" action="Box-function.php">
Delivered by : <input type="text" name="Delivered-by"> <br>
Item No : <input type="text" name="Item-no"> <br>
<input type = "submit">
</form>
Note: I have seen similar questions but those questions involve using MySQL while I am not using it. it's just pure PHP and HTML.
In the box-function.php page, you can get the current date and store it along with the other data.
Getting current date
Do the same when the order is delivered. The difference between the two times will tell you how long the package took to be delivered.
Also, as the comments under your post say, you will have to store the data somewhere. You can't retrieve it later if it isn't stored anywhere.

Href V/s Form submit

I want to delete (or update) a record in MySql through PHP. The options here are :
One.
Delete
Two.
<form action="process.php" method="post"><input type="hidden" name="pid" value="3"><input type="hidden" name="cid" value="10"><button type="submit" class="btn btn-danger">Delete</button></form>
Later, I will redirect from process.php. So, Which is better and may I know why?
In both options, it is working (delete or other). I just want to ask any advantages in form submit
One :- Using get method
Delete
Two :- Using Post method
<form action="process.php" method="post"><input type="hidden" name="pid" value="3"><input type="hidden" name="cid" value="10"><button type="submit" class="btn btn-danger">Delete</button></form>
Compare Get and post method :
Get Method ( Advantages and disadvantages )
Since the data sent by the GET method are displayed in the URL, it
is possible to bookmark the page with specific query string values.
The GET method is not suitable for passing sensitive information
such as the username and password, because these are fully visible
in the URL query string as well as potentially stored in the client
browser's memory as a visited page.
Because the GET method assigns data to a server environment
variable, the length of the URL is limited. So, there is a
limitation for the total data to be sent.
Post method ( Advantages and disadvantages )
It is more secure than GET because user-entered information is never
visible in the URL query string or in the server logs.
There is a much larger limit on the amount of data that can be
passed and one can send text data as well as binary data (uploading
a file) using POST.
Since the data sent by the POST method is not visible in the URL, so
it is not possible to bookmark the page with specific query.
thanks

How to prevent multiple forms having the same input type=hidden values?

I have developed a search form that searches a large SQL database using PHP and shows the results to the final user.
There is a GET request going on and then the URL looks like this:
http://localhost/search.php?value1=x&value2=y
Now, aside from showing the search results to the user, I want to show some options that they can
apply to the shown results.
For example, I will have an export to excel button on top of the results, as well as a delete button in order to
delete the results from the database. In my server, those 2 actions will be managed by 2 different files, say
deleteEntries.php and excelExport.php.
So, in my html I will have 2 forms, one pointing at deleteEntries.php and one pointing at excelExport.php.
The problem is that each form needs to have its own inputs type=hidden repeated.
The code
<input name="value1" value="x" type="hidden">
<input name="value2" value="y" type="hidden">
, which is dynamically generated through PHP, must be repeated for each form (how else can each server-side file know what the
user needs to export or to delete?). The code is dynamically generated because the search criteria and their values will differ
between searches.
Then, if the user chooses to delete the data, the URL will become
http://localhost/deleteEntries.php?value1=x&value2=y
In my specific occassion I have 3 forms and up to 11 values. So this means that I repeat 11 lines of HTML 3 times and I really don't
like that for many reasons.
One obvious solution would be a combobox where I let the user choose the action (e.g. export data, delete data, option 3)
and to manage all the actions with a single form and with a single server-side file by determining the selected action.
But I don't want to do this because it is less user-friendly and less developer-friendly.
Edit: The reason why I don't make a link for Export to excel like
Export to excel
is because in reality user is given more options that have to be selected from a form.
You can define the form action on a submit button as follows:
<form method="post">
<input name="value1" value="x" type="hidden">
<input name="value2" value="y" type="hidden">
<button type="submit" formaction="deleteEntries.php">Delete Entries</button>
<button type="submit" formaction="excelExport.php">Excel Export</button>
</form>
Each submitbutton will post to a different php script
This does not work in IE9 and earlier but I do not know if that is a problem
You could move the list of hidden inputs to a separate file and include that file in each form template.
Or you could have a list of your parameter names in an array in php. Then generate the hidden inputs from that. That way you can just have a single function call in each form to add in the hidden inputs. You can also loop over the same array in the form handler to read and validate the values received before they are used.
You can define onclick function to the buttons rather than sending the form and on the onclick function, you define 2 or more hidden hidden fields to the form you like which means you dont have declare those input on the page load but define it when the onclick function.
var y = document.createElement('value1');
document.form1.appendChild(y);
<form name = "form1" id = "form1" action = "deleteEntries.php" method = "post">
<button type = "submit" name="submit1" onclick="addinput()">DELETE</button
</form>
This the hidden input need not be included in all 3 forms and these fields can be generated dynamically on the particular form button onclick.

PHP Cycle Through MySQL Table With Submit Button

Okay, I've looked for a solution to this question, but to no avail. It could be the way I worded it, but I've tried almost everything I could think of. And I know this is probably something so simple, I just can't wrap my head around it.
The problem I'm having is this:
I have a form that is prepopulated with data from a MySQL database. This part of the form is strictly for viewing the records (that were previously entered by the user). I can get it to work; I have the database linked, and it shows the data for the row of the particular table that I want. But this is what I'm having trouble with.
I want there to be a button that the user can press that cycles through each row of data in the database, and outputs it in the form accordingly.
It's a little complicated, so I'll use a simple example.
Let's say I have a basic table in a MySQL database with three columns: ID, Name,
and EmailAddress.
Here's the query that grabs the data:
$sql = "SELECT * from tbl_Users ORDER BY ID ASC";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
Now I know this is deprecated, but I am working in an older version of php, and I'm trying to stay consistent other pages/apps in this particular domain.
Now let's say I have a simple form with two inputs and a submit button.
HTML
<form name="button" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="Name" value="<?php echo $row[Name]; ?>" />
<input type="text" name="emailAddress" value="<?php echo $row[EmailAddress]; ?>" />
<input type="submit" name="submit" value="Next Record" />
</form>
Now I can get it to echo what I need in the form. It can prepopulate the form with a row, as I want it to. The trouble lies with the submit button.
When the submit button is clicked, I want the next row to populate in the form, and so on until the end of the records.
I've tried the if(isset($_POST['submit']{...}, yet I don't know exactly to get this to work. I've tried loops, etc. I know there is a way, I just cannot comprehend one right now. Thanks in advanced.
Okay, so I DID manage to get it to work with PHP. I found a simple PHP Pagination script and changed the limit of the query to 1. Instead of echoing out the results of the query in the WHILE loop, I just called the variables for the form
Unfortunately in your case, PHP is server side, and it will run any queries before the client side has a chance to catch up and output it. I believe you will need to use javascript for this.
You can do one of two things, 1. Pull all table information on load (slower way) and use javascript to show/hide certain elements of that query. Or 2. You can use AJAX to pull the data on command.
I would suggest learning javascript (e.g. use a framework like jQuery) to perform an AJAX call for this.

Separate save actions jQuery and PHP

I have a form for saving the attendance of students to the MySQL database.
Students' data is read from the database and create the data entry form.
The form is designed to store the code, name, presence status of each student and a short description in case of absence.
I have designed the data entry form, but seem to need some clues regarding the save action so that clicking on each save button, the appropriate data is stored in the attendance table of the database.
If you don't care about old IE (which has bugs in its support of <button>, I think version 7 fixes this those bugs) then (making sure you are in Standards mode):
<button type="submit" name="row_id" value="insert row id here">Save</button>
$row_id = $_POST['row_id'];
If you do care about old IE:
<input type="submit" name="row_id_row_id_here" value="Save">
and loop over the keys in $_POST looking for ones which match the regex /^row_id_(.*)/.

Categories