good day all ,, I am new learner and trying to make an associative array for jobs which user inputs the id, title and description but it is not correct ,,can u guide me through this ?
I also want to search for jobs by its title or description and return the job id ,
Thanks alot
<html>
<body>
This form is for storing array of jobs with ID and description for each
<form method = "post" >
input job iD <input id="jobid">
input jobname <input id="jobname">
Write a description <input id="jobdesc">
<input type="submit" value="click to store input" >
</form>
</body>
</html>
<?php
$jobs_array = array();
$jobs_array[] = array ($_POST['jobid'] ,$_POST['jobname'], $_POST['jobdesc']);
?>
You do not need to separate the values like
$_POST['jobid'] ,$_POST['jobname'], $_POST['jobdesc']
and enclose them in an array. Because, they are originally formed that way. When a user submits a post with multiple values, all those values are stored in the super global array $_POST so, instead of separating and then, attaching them inside an array, just depend on this one only, because it has all you need inside.
$all_arrays = $_POST;
Tweaked your markup a bit to
<html>
<body>
<p>This form is for storing array of jobs with ID and description for each </p>
<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post" >
<p><label for = "jobid">input job iD</label> <input type = "text" name = "jobid" id="jobid"></p>
<p><label for = "jobname">input jobname</label><input type = "text" name = "jobname" id="jobname"></p>
<p><label for = "jobdesc">Write a description</label><input type = "text" name = "jobdesc" id="jobdesc">
<input type="submit" value="click to store input" >
</form>
</body>
</html>
<?php
$jobs_array = array ($_POST['jobid'] ,$_POST['jobname'], $_POST['jobdesc']);
?>
you can access jobid with $jobs_array[0] now, and so on.
An associative array is one where you have a value in an array which can be accessed by a key - that acts as the index.
In your code, as shown below, you are assigning a value to the array without a key thus it isn't associative. Furthermore, you are adding an array to the array making it multidimensional which is inappropriate in this situation.
$jobs_array[] = array ($_POST['jobid'] ,$_POST['jobname'], $_POST['jobdesc']);
The code should look like this:
$jobs_array = array("job_id" => $_POST['jobid'], "job_name" => $_POST['jobname'], "job_description" => $_POST['jobdesc']);
Also, the reason why the $_POST variables are not set is because you're using id rather than name. id refers to the stylesheet whereas name refers to how the data in the field can be accessed.
For the second part of your question, you need to be using a database to store the jobs, and from there, you can run queries whereby you are able to search through the rows by its id, and return an array of results.
Related
Started learning PHP today so forgive me for being a noob. I have a simple HTML form where the user inputs 4 strings and then submits.
HTML form
<html>
<head>
<title>EMS</title>
</head>
<body>
<h1>EMS - Add New Employees</h1>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
<table>
<tr><td>Enter a NAME:</td><td> <input type="text" name="name"></td></tr>
<tr><td>Enter a PPSN:</td><td> <input type="text" name="ppsn"></td></tr>
<tr><td>Enter a PIN :</td><td> <input type="text" name="pin"></td></tr>
<tr><td>Enter a DOB:</td><td> <input type="text" name="dob"></td></tr>
<tr><td></td><td><input type="submit" value="Add New Employee" name="data_submitted"></td></tr>
</table>
</form>
</html>
I want to implode the 4 elements in the $_POST["data submitted"] array to a string.
PHP
<?php
if (isset($_POST['data_submitted'])){
$employee = implode(",",$_POST['data_submitted']);
echo "employee = ".$employee;
}
?>
Why is it that when I run the project, Input the 4 strings into the form and submit that there is nothing contained within the employee string when its outputed? There is however a value in the employee string when I just implode the $_POST array like so without 'data_submitted'.
$employee = implode(",",$_POST);
The output of the $employee string is now - employee = will,03044,0303,27/5/6,Add New Employee
It contains the name,pps,pin,dob and this ADD New Employee value?
How do I just get the $employee string to contain just the name,pps,pin and dob from the $POST_[data_submitted] array?
If you wish to implode the submitted data, then you need to refer to the specific items, as follows:
<?php
$clean = [];
if (isset($_POST['data_submitted'])){
// one way to deal with possibly tainted data
$clean['name'] = htmlentities($_POST['name']);
$clean['ppsn'] = htmlentities($_POST['ppsn']);
$clean['pin'] = htmlentities($_POST['pin']);
$clean['dob'] = htmlentites($_POST['dob']);
$employee = implode(",",$clean);
echo "employee = $employee";
}
Never use submitted data without first checking to make sure that it is safe to do so. You need to validate it. Since the OP doesn't specify what kind of data the named inputs "ppsn", "pin", "dob" pertain to, this example does a minimum of validation. Each input might require more or something different.
Whether you're new or familiar with PHP, it is a good idea to frequently read the online Manual.
First, you need to know that php will treat value in the format: value="value here" as string.
So, calling implode(",",$_POST['data_submitted']); will return Add New Employee as declared here: <input type="submit" value="Add New Employee" name="data_submitted">.
From your question:
How do I just get the $employee string to contain just the name, pps, pin and dob from the $_POST[data_submitted] array?
Solution
1. Unset the <code>$_POST['data_submitted']</code> index in the $_POST super global variable
2. Implode it
// Unset the $_POST['data_submitted'] index
$post_data = unset( $_POST['data_submitted'] );
// Format the post data now
$format_post_data = implode( ",", $post_data );
// Escape and display the formatted data
echo htmlentities( $format_post_data, ENT_QUOTES );
I have this form
<form action="process.php" method="post">
Team Name: <input type="text" name="teamname" />
<input type="submit" />
</form>
and this is my php code
$teamname = $_POST['teamname'];
$namelist = "Lakers";
so let's say two people have submitted their team names as Spurs and Rangers
so how do I make the namelist like this and grow as more people submit their team names..
$namelist = "Lakers, Spurs, Rangers";
I have done it in array_push with arrays, but technically i can't call them.
you need to save the names to file/database.
variable save in the memory of the machine and deleted after the scripts done.
$names = array(); // empty array
$names[] = $_POST['teamname']; // add the $_POST['teamname'] to the array
var_dump($names); // prints the names array.
// now the script done, and all the data in the variables will flush from the memory.
You can use array to solve this issue
<?php
$teamname = $_POST['teamname'];
$namelist = $array("Lakers");
array_push($namelist,$teamname);
print_r($namelist);
?>
using database is a simple solution.
but if you still not like to use database in this situation, you can save the variables as a session or cookie variable. it will remain after page refresh.
I have two pages:
Graph.php
List.php
The Graph page does exactly what it is named, graphs data. If there is no post/get data it displays all the data in a given table.
The List page is a huge table which loads around 500-600 rows of data. In the table you can sort and filter the rows using JavaScript. The table is around 14 columns wide.
After sorting the rows in the List page you can press a button 'Graph' that will take the visible rows and graph them on the graph page.
What I am having trouble with is passing these ID's over to the graph page. I started with:
<?php
if(isset($_POST['data']))
{
echo "FOUND SERIALIZED ARRAY<br>";
$afterSerializeArray = unserialize($_POST['data']);
print_r($afterSerializeArray);
}
$beforeSerializeArray = array();
$beforeSerializeArray[] = 1;
$beforeSerializeArray[] = 2;
$beforeSerializeArray[] = 3;
$serializeArray = serialize($beforeSerializeArray);
?>
<form action="" method="post">
<input type="hidden" name="data" value="<?php echo $serializeArray; ?>"/>
<input type="submit" value="Serialize"/>
</form>
I have written the small snippet to grab the ID's of the visible rows and load them into an array, serialize it and pump it into a variable to post it over to the graph.
Should I be using GET? Should I be doing this a different way?
The reason I wanted the filter and sort on a different page than the graph is because users have a lot of columns and options to filter and sort by.
Rather than trying to send array over post you should concatenate these ids with any special character (say ','). This way you will get all IDs as comma separated values in $_POST['data']. Now you can use PHP explode function to get all the values in an array and use them as you wish.
This code sample might help you
<?php
if(isset($_POST['data']))
{
echo "FOUND Ids<br>";
$IdArray = explode(',',$_POST['data']);
print_r($IdArray );
}
$idarray = array('1','2','3');
$ids = implode(',',$idarray);
?>
<form action="" method="post">
<input type="hidden" name="data" value="<?php echo $ids;?>"/>
<input type="submit" value="Serialize"/>
</form>
Ive started working on a dynamic form script that allows a user to add form elements via Jquery, which is then in turn submitted to a PHP script.
I'm just after some feedback on ways to achieve this. At the moment I have the following:
When a user adds a form element the element is added with the following name array:
<textarea name="element[text][123]">
<input type="text" name="element[input][456]" />
As I need to know the type of form element that was submitted I am using a multidimensional array called 'element[][]' where the first level of the array is the type of element and the second element of the array is a unique ID and the value.
When I var_dump() This after submission PHP outputs:
array
text => array
123 => string 'The textarea value'
input => array
456 => string 'The input field value'
Im working on the PHP side of the script now and just wondering if there is a better way to do this.
Any thoughts?
UPDATE
I have to change the way that Im doing this as the array keynames are not unique.
If the user adds two textareas
<textarea name="element[text][123]">
<textarea name="element[text][456]">
When the user adds a form element, the element can be dragged so the positioning can be changed after the element was created. This allows a user to add an element but then move it to where they want it to appear.
PHP handles this ordering fine and accepts the array in the order that the form is submitted, however as mentioned above if the key names are the same then the order will be broken.
On the PHP side I need to know
the type of form field
the value of the form field
the unique ID, which is just a timestamp, of the form field
I think I might need to do what Cole mentioned, assigning the names as:
element[text_123]
I can then explode the keyname on '_' to determine the type and the identifier.
UPDATE
I took the script Jack posted and slightly modified it
$vars = $_POST['element'];
foreach ($vars as $id => $vals)
{
// $vars[id] outputs the ID number
// $vars[vals] is the array containing the type and value
echo "This fields ID is $id. ";
foreach($vals as $key => $value)
{
echo "Type was: $key and the value was: $value <br />";
}
}
A quick test of this outputted
This fields ID is 1338261825063. Type was: heading and the value was: xzczxczxczxczxczxc
This fields ID is 1338261822312. Type was: heading and the value was: asdasdasdasdad
From this I know the identifier and the array that it belongs to, the type and the value, but I also know the order that the data was submitted.
From that I can wrap my data in markup, perform any additional operations and then insert the data into the database.
Looks okay; you could also consider something like this (it introduces more fields though, so you must really think the benefit is worth it):
<input type="hidden" name="element[123][type]" value="text" />
<input type="hidden" name="element[456][type]" value="input" />
<textarea name="element[123][value]">
<input type="text" name="element[456][value]" />
Then you can do this:
foreach ($_POST['element'] as $name => $info) {
// $info['type'] is 'text' or 'input'
// $info['value'] is the user input
}
This is more of a technique question rather than maybe code. I am having a php form with many fields (items to select). Naturally some of the items might be selected and some not. How do I know which ones are selected when i post the data from page 1 to page 2? I thought of testing each one if empty or not, but there are just too many fields and it doesn't feel at all efficient to use or code.
Thanks,
UPDATE EDIT:
I've tried the following and maybe it will get me somewhere before I carry on testing the repliers solutions...
<html>
<body>
<form name="test" id="name" action="testprocess.php" method="POST">
<input type="text" name="choices[shirt]">
<input type="text" name="choices[pants]">
<input type="text" name="choices[tie]">
<input type="text" name="choices[socks]">
<input type="submit" value="submit data" />
</form>
</body>
</html>
and then second page:
<?php
$names = $_POST['choices'];
echo "Names are: <br>";
print_r($names);
?>
This gives out the following:
Names are: Array ( [shirt] => sdjalskdjlk [pants] => lkjlkjlk [tie]
=> jlk [socks] => lkjlkjl )
Now what I am going to try to do is iterate over the array, and since the values in my case are numbers, I will just check which of the fields are > 0 given the default is 0. I hope this works...if not then I will let you know :)
I think what you're looking for is this:
<form action="submit.php" method="POST">
<input type="checkbox" name="checkboxes[]" value="this" /> This
<input type="checkbox" name="checkboxes[]" value="might" /> might
<input type="checkbox" name="checkboxes[]" value="work" /> work
<input type="submit" />
</form>
And then in submit.php, you simply write:
<?php
foreach($_POST['checkboxes'] as $value) {
echo "{$value} was checked!";
}
?>
The square brackets in the name of the checkbox elements tell PHP to put all elements with this name into the same array, in this case $_POST['checkboxes'], though you could call the checkboxes anything you like, of course.
You should post your code so we would better understand what you want to do.
But from what I understood you are making a form with check boxes. If you want to see if the check boxes are selected, you can go like this:
if(!$_POST['checkbox1'] && !$_POST['checkbox2'] && !$_POST['checkbox3'])
This looks if all the three check boxes are empty.
Just an idea:
Create a hidden input field within your form with no value. Whenever any of the forms fields is filled/selected, you add the name attribute of that field in this hidden field (Field names are saved with a comma separator).
On doing a POST, you can read this variable and only those fields present in this have been selected/filled in the form.
Hope this helps.
Try this.....
<?php
function checkvalue($val) {
if($val != "") return true;
else return false;
}
if(isset($_POST['submit'])) {
$values = array_filter(($_POST), "checkvalue");
$set_values = array_keys($values);
}
?>
In this manner you can get all the values that has been set in an array..
I'm not exactly sure to understand your intention. I assume that you have multiple form fields you'd like to part into different Web pages (e.g. a typical survey form).
If this is the case use sessions to store the different data of your forms until the "final submit button" (e.g. on the last page) has been pressed.
How do I know which ones are selected when i post the data from page 1 to page 2?
is a different question from how to avoid a large POST to PHP.
Assuming this is a table of data...
Just update everything regardless (if you've got the primary / unique keys set correctly)
Use Ajax to update individual rows as they are changed at the front end
Use Javascript to set a flag within each row when the data in that row is modified
Or store a representation of the existing data for each row as a hidden field for the row, on submission e.g.
print "<form....><table>\n";
foreach ($row as $id=>$r) {
print "<tr><td><input type='hidden' name='prev[$id]' value='"
. md5(serialize($r)) . "'>...
}
...at the receiving end...
foreach ($_POST['prev'] as $id=>$prev) {
$sent_back=array( /* the field values in the row */ );
if (md5(serialize($sent_back)) != $prev) {
// data has changed
update_record($id, $sent_back);
}
}