sisimh
sisimh

Reputation: 1337

associative array with web form

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']);
?>

Upvotes: 0

Views: 1583

Answers (3)

Nick
Nick

Reputation: 213

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.

Upvotes: 0

samayo
samayo

Reputation: 16495

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;

Upvotes: 1

max_
max_

Reputation: 24481

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.

Upvotes: 0

Related Questions