skip to Main Content

I have a script that will take a page name and change the form action, which will then take me to that page. However, I also want the ID for the page content to be passed to it. I assumed that a hidden field would work. I get the page, but there is no content.

<script language="javascript">
<!-- Script courtesy of http://www.web-source.net - Your Guide to Professional Web Site Design and Development
function goto(form) { var index=form.select.selectedIndex
if (form.select.options[index].value != "0") {
location=form.select.options[index].value;}}
//-->
</script>
<form name="form1">
<input type="hidden" name="id" id="id" value="<?php echo $row['id'];?>">
<select name="select" onchange="goto(this.form)">
<option value="">-------Choose a Selection-------
<option value="view.php">View
<option value="download.php">Download
</select>
</form>

I then capture the ID on the receiving page with.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $app_id = $_POST['id'];
}

2

Answers


  1. You can do this to change the form method based on a condition in PHP. About the hidden field, your solution implementation looks reasonable (provided I don’t have more details on what you are trying to do).

    <?php
    
    $yourcondition = false;
    
    $formMethod = $yourcondition ? 'post' : 'get';
    // Placeholder echo so you can verify your condition actually works.
    echo($formMethod)
    ?>
    
    <form action="your_action.php" method="<?php echo $formMethod; ?>">
        <input type="text" name="exampleField" required>
        <input type="submit" value="Submit">
    </form>
    
    Login or Signup to reply.
  2. The default form is a GET request.
    So you only need to add method="POST"

    <form name="form1" method="POST">
    

    Or when receiving data in PHP, change it to:

    $app_id = $_GET['id'];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search