Moms
Moms

Reputation: 165

How to use values in the URL in PHP

I am currently making a report error form that has 4 fields:

  1. Job ID $jobid
  2. Part ID part_id
  3. Machine
  4. Note

The user clicks on a table corresponding the their work and are brought to a new page with a url that has variable. At the moment all the fields are empty however I want the fields to be populated automatically except for notes.

Current Model

Link to report error form:

$EM_html = '<a href="/report_error_form.php?JobID='.$jobid.'&Machine=EM&PartID='.$part_id.'">'.$tick.'</a>

Report error form:

<form action="" method="post">
        Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
        Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
        Machine Code: <input type="text" name="machCode"><br>
        Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
        <input type="submit" name="submit" value="Submit">
</form>

Example URL

http://sra-pstest/report_error_form.php?JobID=KANBAN16-09-04-01&Machine=EM&PartID=124047 How do "extract" the information out of the url (JobID, Machine, PartID) and automatically fill out the form?

Upvotes: 1

Views: 69

Answers (5)

Gyan
Gyan

Reputation: 508

Try using isset and post method to check if variable are declared and get the variable data on submit of form

<?php
    if(isset($_POST['submit'])){
      $jobid = $_POST['JobID'];
      $part_id = $_POST['PartID'];
      $machCode = $_POST['Machine'];
    }

?>

<form action="" method="post">
     Job Number: <input type="text" value="<?php echo $jobid; ?>" name="jobNum"><br>
     Part Number: <input type="text" value="<?php echo $part_id; ?>" name="partNum"><br>
     Machine Code: <input type="text" name="machCode" value="<?php echo $machCode; ?>"><br>
     Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
     <input type="submit" name="submit" value="Submit">
</form>


Hope this help

Upvotes: 0

Peeje
Peeje

Reputation: 475

 <form action="" method="post">
    <?php $jobNumber = isset($_GET['JobID']) ? $_GET['JobID'] : '' ?>
    Job Number: <input type="text" value="<?php echo jobNumber; ?>" name="jobNum"><br>
    <input type="submit" name="submit" value="Submit">
</form>

Upvotes: 0

Vadivel S
Vadivel S

Reputation: 660

You use $_GET Method like this code

<?php 
$jobid=$part_id=$machine="";
if(isset($_GET['JobID']))
{
    $jobid= $_GET['JobID'];

}
if(isset($_GET['Machine']))
{
    $machine= $_GET['Machine'];

}
if(isset($_GET['PartID']))
{
    $part_id= $_GET['PartID'];

}

?>

Upvotes: 0

Divyesh Savaliya
Divyesh Savaliya

Reputation: 2740

You can use $_GET

<?php 
    if(isset($_GET))
    {
        foreach($_GET as $key=>$value)
        {
            $$key=$value;
        }

        echo $JobID."<br>".$Machine."<br>".$PartID;
    }

?>

Upvotes: 1

dhi_m
dhi_m

Reputation: 1265

Please try this

<?php
        $jobid    = @$_REQUEST['JobID'];
        $part_id  = @$_REQUEST['PartID'];
        $machCode = @$_REQUEST['Machine'];
 ?>


    <form action="" method="post">
                Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
                Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
                Machine Code: <input type="text" name="machCode"><br>
                Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
                <input type="submit" name="submit" value="Submit">
    </form>

Upvotes: 0

Related Questions