Jump to content

Passing arrays via URL


Koufax

Recommended Posts

Hey everyone,

 

So this is my first time posting and I hope that I can get some feedback pertaining to my issue.  Here is a brief background of the project that I am working on:

 

My company is asking me to create a system that helps keep track of all tracking numbers by division.  So for the past few days, I have gotten a good jump on the project.  I created a user registration page, a log in page, a lost password page, and a page that shows the current status of all tracking numbers already in our system.

 

However, I have now been asked to create a page where a user can add tracking numbers to the system.  The user is to scan the tracking number's bar code as input.  Since the scanning gun is the same input as the keyboard, the page recognizes it right away.  Upon scanning the tracking number, it is to be displayed on the page as a table.  This process is to be repeated until the user clicks on the "Finish" button.

 

In other words, the page receives input from the scan gun, and upon scanning the barc ode, it is added to an array which I intend on displaying its contents as a simple HTML table.  My problem is that every time the tracking number is submitted, the array gets erased.

 

In a nutshell, this is the code that I have so far:

 

<?php

$tracking_array = array();
if (isset($_POST['submitted']))
{
	if (isset($_POST['tracking_id']))
		$tracking_id = $_POST['tracking_id'];
	else
		$tracking_id = NULL;

	array_push($tracking_array, $tracking_id);
	//printf("%s", $tracking_id);
	print_r($tracking_array);
}
?>


<form name='add_form' method='post' action ='add.php'>
<fieldset>
	<legend>Add a Tracking Number</legend>
	<center>Tracking Number: <input type='text' name='tracking_id' id='tracking_id' />
	<input type='submit' value='Add' name='add' id='add' /></center>
	<input type='hidden' value='TRUE' name='submitted'/>
</fieldset>

<script language="JavaScript"> 
	document.add_form.tracking_id.focus()
</script>
</form>

 

Any help would be greatly appreciated.  I apologize ahead of time for my lack of form/PHP knowledge. :)

 

Link to comment
https://forums.phpfreaks.com/topic/197073-passing-arrays-via-url/
Share on other sites

You're a lifesaver!  I think I got it to work successfully.  This code works for me so far:

 

<?php

session_start();

if (!isset($_SESSION['tracking_session']))
    	$_SESSION['tracking_session'] = array();

if (isset($_POST['submitted']))
{
	if (isset($_POST['tracking_id']))
		$tracking_id = $_POST['tracking_id'];
	else
		$tracking_id = NULL;

	array_push($_SESSION['tracking_session'], $tracking_id);

	if (isset($_POST['finish']))
	{
		$_SESSION['tracking_session'] = NULL;
	}
}
?>


<form name='add_form' method='post' action ='add.php'>
<fieldset>
	<legend>Add a Tracking Number</legend>
	<center>Tracking Number: <input type='text' name='tracking_id' id='tracking_id' />
	<input type='submit' value='Add' name='add' id='add' />
	<input type='submit' value='Finish' name='finish' id='finish' /></center>
	<input type='hidden' value='TRUE' name='submitted'/>
</fieldset>

<script language="JavaScript"> 
	document.add_form.tracking_id.focus()
</script>
</form>


<?php

if (!(empty($_SESSION['tracking_session'])))
{
	printf("<table border='1'><tr><td>Tracking Number</td></tr>");		

	for ($i = sizeof($_SESSION['tracking_session']); $i >= 0; $i--)
		printf("<tr><td>%s</td></tr>", $_SESSION['tracking_session'][$i]);

	printf("</tr></table>");
}

?>

 

 

I have the session array set to NULL when the user clicks on the finish button.  Also, I was asked to have the latest added number to be displayed first on the table, so I ran a for loop that begins with the last element in the array.  If there is anything wrong or if there is a better way that I can go about processing this data, I would be more than happy to hear any suggestions.

 

Thanks again for all the help.  I'll be sure to post in here a bit more often now.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.