简体   繁体   中英

How to send value of radio button to other page

I have a table with radio buttons to get the row values and 2 buttons

1 button.)For printing data , which moves to "notice.php"

2 button.)For row details,which stays on the same page.

<form action="" method="POST">
<table border="1" >
    <tr>
    <th>sourceID</th>
    ....
    <th>Status</th>
    </tr>
        <tr>
            <td><input type="radio" name="ID[]" value="<?php echo $tot; ?>" /></td>
            <td>1</td>
            ....
            <td>open</td>
    </tr>
<input type="button" name="issue" value="Issue Notice"  onClick="location.href='notice.php'"  />
    <input type="submit" name="details" value="details"  />
    <?php
    if(isset($_POST['details']))
    {
    $n=$_POST['ID'];
$a=implode("</br>",$n);
echo$a;
    }

Notice.php:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n=$_POST['ID'];

}?>

The problem here is: My code is working perfectly fine with the button details. But it doesnt work with issue, ie after selecting radio button and clicking on the issue notice button :it gives Undefined index: ID in D:\\XAMPP\\notice.php. kindly help

Your details button is a submit button, so it submits the form. However your other button is just a regular button and you use javascript to send the browser to notice.php. As such, it does not post any data to notice.php.

You could include the data on the query string and send it that way, eg:

location.href="notice.php?id=<?=$tot?>"

Or you could also have the issue button post the page, and then have your receiving page check which submit button was used. If the issue button was used you could then have the php code post to notice.php.

Using the following code is the exact same as having a link:

<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />

As in, this will not change the form action and submit the POST data to your new page.

You would need something like:

<form method="post" action="" name="unique-form-name">
    <input type="radio" name="ID[]" value="<?php echo $tot; ?>">
    <input type="button" id="unique-btn-name" value="Issue Notice">
</form>

<script type="text/javascript">
document.getElementById('unique-btn-name').onclick = function(){
    document['unique-form-name'].action='notice.php';
    document['unique-form-name'].submit();
}
</script>

Then, once you get the data to notice.php , you'll have to use the data as an array (you won't be able to echo the data):

$IDs = $_POST['ID'];
echo '<pre>',print_r($IDs),'</pre>';
<input type="radio" name="ID" value="<?php echo $tot; ?>" />

Your error is the name attribute.

Also the other button is not related to the form at all. You may want to use ajax here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM