2

I wonder whether someone may be able to help me please.

Firstly apologies as I am new to this, so to some this may seem a very basic error.

I'm using the form below to allow users to navigate between different pages all based around a 'Location' record.

<form name="locations" id="locations" method="post" action="locationsaction.php">

    <?php

        <table width="538" border="0">
        <tr>
        <th width="119"><div align="left">Location Name</div></th>
        <th width="159"><div align="left">Address</div></th>
        <th width="94"><div align="center">No. Of Finds</div></th>
        <th width="69"></th>
        <th width="66"></th>
        <th width="5"></th>

        </tr>
        <tr>
        <td><div align="left"><?php echo $lid;?></div></td>
        <td><div align="left"><?php echo $lname;?></div></td>
        <td><div align="left"><?php echo $laddress;?></div></td>
        <td><div align="center"><?php echo $findscount?></div></td>
        <td><div align="center"><input name="type" type="submit" value="View Details"/></div></td>
        <td><div align="center"><input name="type" type="submit" value="Add Finds"/></div></td>
        <td><div align="center"><input name="type" type="submit" value=" Add Images"/></div></td>
        </tr>
        <tr><th colspan="8"><br/><hr width="100%"></th></tr>
        </table>
        <?php
</form>

At the end of the form you will notice that there are three 'Submit' buttons which takes the user to different pages. The following PHP script is used to determine which button has been selected and is shown in the above Form POSTaction as locationsaction.php

<?php
if (isset($_POST['type'])) {
    $urls = array(
        'View Details' => 'updatelocation.php',
        'Add Finds' => 'addfinds.php',
        'Add Images' => 'image.php'
    );
    $url = $urls[$_POST['type']];
    header("Location: " . $url);
}
?>

Although the buttons work fine, as in they take the user to the correct page, I cannot get the $lid variable to be passed to any of the three receiving pages and I receive the following error: Notice: Undefined index: lid in /homepages/2/d333603417/htdocs/updatelocation.php

This is how I'm trying to get the $lid variable in all my receiving pages:

$lid = $_POST['lid'];

I've looked through several tutorials and posts on this site, and I can't find out where I'm going wrong.

I just wondered whether someone could have a look at this and let me know where I'm going wrong?

Many thanks and kind regards

1
  • Why submit and redirect when you could put the links right in the page? You aren't actually submitting any data... Commented Jun 14, 2012 at 14:18

7 Answers 7

1

First, put the $lid variable in an input:

<input type="hidden" name="lid" value="<?=$lid?>" />

Otherwise it won't be posted in the form.

Also, when you redirect, the POST variables won't be redirected with you. You can either append them to the url (http://url.tld?lid=value), and then get at them like so:

$lid = $_GET['lid'];

However, a better way might be to save them in a session:

$_SESSION['lid'] = $_POST['lid'];
// Redirect here

Then on the next page, you can access them:

$lid = $_SESSION['lid'];
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Toddish thank you for taking the time to look at my post, but more importantly for taking the time to help me. It works great. Many thanks. Kind regards
1

You're not passing the lid value through POST to any of the scripts. If you want to pass it, put something like this in your form:

<input type = "hidden" value = "<?= $lid ?>" name = "lid" />

1 Comment

Hi @robbrit, thank you very much for helping me out with this. It's truly appreciated. Kind regards
1

Everything you want to post has to be in an input (or select or textarea) and have a name.

That is not so in your form.

2 Comments

Hi @Neal I just want to say thank you for taking the time to look at my post, but more importantly for taking the time to help me. It is truly, greatly appreciated. Kind regards
@IRHM no problem, ^_^ anytime.
1

You need to use a form element containing the $lid variable. Something like this:

<input type="hidden" name="lid" value="<?php echo $lid; ?>" />

1 Comment

Hi @curtisdf, thank you for taking the time to reply to my post. I've got this working perfectly now. Kind regards
1

your $lid variable is sent through to locationsaction.php but not to updatelocation.php,

to go around this, you could define $_SESSION['lid'] = $lid; in the locationsaction.php

1 Comment

Hi @Wampie Driessen, thank you very much for this. Along with the other guidance I've received, I've got this working perfectly. Kind regards
1

Ok, so schematically here is your architecture:

form > locationsaction.php > updatelocation.php (or another one, depends on the button)

You have access to POST into locationsaction.php because you actually posted datas, using the submit buttons of your form. But now if you need datas in updatelocation.php, you can't use $_POST to retrieve it unless you POST datas again.

Instead, I recommend you use $_SESSION array to store values and use them through your different php pages.

1 Comment

Hi @Sebas, thank you for taking the time to reply to my post. I've got this working perfectly now. Kind regards
1

The inputs elements are mandatories (hidden etc..) for pass value with form. If the locationaction.php isn't so really impotant to use, i suggest you to change directly action fom into the first page.

<td><div align="center"><input name="type" type="submit" onclick="window.locations.action='updatelocation.php'" value="View Details"/></div></td>
        <td><div align="center"><input name="type" onclick="window.locations.action='addfind.php'" type="submit" value="Add Finds"/></div></td>
        <td><div align="center"><input name="type" type="submit" onclick="window.locations.action='image.php'" value=" Add Images"/></div></td>
        </tr>

However, when you use an intermediate page to retrives and sends post values you can use this code

<form name="iaminthemiddle" action="finaldest.php" method="post">
<?php
foreach($_POST as $k => $v)
 echo "<input type=\"hidden\" name=\"$k\" value=\"$v\">";
?>
</form>
<script>window.iaminthemiddle.submit();</script>

This for $_POST elements, you can retrive also the querystring values with $_GET or both with $_REQUEST

1 Comment

Hi @Goonie, thank you for this. I've managed to get everything working for the moment using the other method. However I'll set up some test pages so I can try this. Kind regards

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.