HTML form using radio button to pass PHP array on - php

I have created a simple HTML form using radio input, consisting of three inputs, namely ONX, BUDAIR and ECC accordingly. When ONX is clicked and submitted, I need it to pass the array $onx on. I have only included the $onx array for simplicity.
I have managed to get it to work using http_build_query, but I want it to be in a form.
<!DOCTYPE html>
<html>
<body>
<h2>Radio Buttons</h2>
<?php $onx=array("onx_service",2,3,4,5);
print_r($onx); ?>
<br />
<!--<form action="radio.php">
input type="radio" name="service" value="onx"> ONX<br>
<input type="radio" name="service" value="bud"> BUDAIR<br>
<input type="radio" name="service" value="ecc"> ECC<br>
<input type="submit" value="Submit">
</form> -->
<?php echo "<a href='radio.php?" . http_build_query($onx) . "'>Select
ONX service</a>";
echo "<br>";
echo $onx[0] . "<br>";
echo $onx[1] . "<br>";
echo $onx[2] . "<br>";
echo $onx[3] . "<br>";
echo $onx[4] . "<br>";
?>
</body>
</html>
When either of the three radio inputs are clicked, it should pass on the relevant array.
When I manage to get the code working correctly I will add it into the correct file. The "http_build_query($onx)" potion of the code above is a sample of what I am trying to achieve within the form using radio inputs.

Related

Where can i implement a php code in joomla?

So I'm doing this website with Joomla and I don't have much experience with Joomla.
I just want to get the values of some checkboxes and print them into a text area.
I have the script already, I just need to implement it.
Here the raw Html code of the checkboxes:
<div class="checkboxShop">
<form action="" method="post">
<input type="checkbox" value="1" id="checkboxShopInput" name="shop" />
<label for="checkboxShopInput"></label>
</form>
</div>
And Here is the raw Html code of the submit button:
<form action="/Flex/index.php/shop" method="post">
<input type="submit" value="Buy">
</form>
All I want is to get all the values of the checkboxes and add them to a textarea in index.php/shop. I wrote this code to get the values but i don't know where to add this:
<?php
if(!empty($_POST['shop'])) {
foreach($_POST['shop'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
echo "<input type='text' name='message' value='" . $check . "'>"
}
}
?>
I've read that i need this but I'm not sure how: <?php include "name_of_script.php";?>
Thanks for the help

Parse radio button postback data using PHP

I have a question related to php. If anyone have an idea,please share with me. (I am a beginner in php).
I received a button value from an HTML page and displayed corresponding figure in the second page using code "param1.php". In the same program itself ( ie, "param1.php"), there is a set of radio box and i need to receive the radio value using another php program. But here i have confusion how to receive the radio value using another php program say "param2.php". also how the page redirect (to the php program where i receivvee the radio value) on selecting a radio button.
Thanks
I attach the "param1.php" below,
<!DOCTYPE html>
<html>
<body>
<br>
<?php
$data = $_POST['btn'];
$data2=$data.".png";
?>
<img src="<?php echo $data2;?>">
<h3>SCALE</h3>
<input type="radio" name="group1" value="5,10,15" checked> 5,10,15<br>
<input type="radio" name="group1" value="5,9,13"> 5,9,13<br>
</body>
</html>
No form tags
There are no form tags, so your radio tags won't get POST'd to param2.php
<form action="param2.php" method="POST">
<input type="radio" name="group1" value="5,10,15" checked> 5,10,15<br>
<input type="radio" name="group1" value="5,9,13"> 5,9,13<br>
<input type="submit" value="Process" />
</form>
Incorrect $_POST key
Now, in your param2.php file, you will be able to get the value of POST['group1']. In your code you're using the wrong key. btn doesn't exist within $_POST (in the code you've given anyway). The radio button name is group1, so access it as such;
$data = $_POST['group1'];
The value of $data will either be 4,10,15 or 5,9,13. Ensure you've got an image named 4,9,13.png and 4,10,15.png else your image won't show. Because they're comma separated, I'm going to assume these are unique file names. So;
foreach( explode(",", $_POST['group1']) as $file) {
echo "<img src='". $file .".png' />";
}
Also, ensure you do some checks on the posted data to validate the user input

Array of forms in PHP

First, thanks for taking a look at this. I am trying to create an array of forms that acts as a dynamically sized results list. From the results that were given the user can click 'detail' (a submit button) to get further information on the result which is why I am attempting to create an array of forms. Here is what I had tried, which compiled but the buttons aren't doing anything. Any help would be great :)
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?PHP
$numbers=array(1,2,3,4,5);
$listsize=count($numbers);
for($currentnum=0;$currentnum <$listsize;$currentnum ++){
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="button" value="Submit" name="button<?PHP echo $currentnum?>" />
</form>
<?PHP
echo "<br/>";
}
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
?>
</body>
</html>
This is really just meant to demonstrate what I am trying to do (thought that would be easier without functions out of scope of question).
Try changing the HTML for the button:
<input type="button" value="Submit" name="button<?PHP echo $currentnum?>" />
Should be:
<input type="submit" value="Submit" name="button<?PHP echo $currentnum?>" />
Close bracket for your for loop so that your check is inside it:
<?PHP
echo "<br/>";
}
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
?>
Should be:
<?php
echo "<br/>";
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
}
?>
If you learn to indent your code you'll find these kinds of bugs much easier to spot!
Other than that you're good to go...
You need to change your input types from 'button' to 'submit' so that they submit the forms, then you need to move
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
inside of the for loop

this.form.submit() with PHP isset?

echo "<form method=\"post\" action=\"settings.php\" onchange=\"this.form.submit()\">";
echo "Announce New Files: <input type=\"radio\" name=\"announcefiles\" value=\"On\" $checkon1> On";
echo "<input type=\"radio\" name=\"announcefiles\" value=\"Off\" $checkoff1> Off<br>";
echo "</form>";
I am trying to get this form to submit when one of the radio buttons is pressed but I'm not sure how to catch the submission.
for example, normally with a submit button you would use something along the lines of if(isset($_POST['submit'])) but I'm not sure how to do it if the form auto submits.
Add hidden input field like:
<input type="hidden" name="action" value="submit" />
Then in PHP check:
if(isset($_POST["action"]) && $_POST["action"] == "submit") { ... }
You should be checking the request method. If you've set things up cleanly, a POST request at that URL will mean a form submit. As you've noticed, you can have attempted submits where a value just isn't there.
if ($_SERVER['REQUEST_METHOD'] === 'POST')
See $_POST vs. $_SERVER['REQUEST_METHOD'] == 'POST' for more discussion.
Give your form a name and check for isset($_POST['form_name']) or check for the name of the radio isset($_POST['announcefiles'])
Also, you don't need all the quote escaping that you have, you can use single quotes as well as use a multiline string - see example below.
echo "
<form method='post' name='form_name' action='settings.php' onchange='this.form.submit()'>
Announce New Files: <input type='radio' name='announcefiles' value='On' $checkon1> On
<input type='radio' name='announcefiles' value='Off' $checkoff1> Off<br>
</form>";
<?php
// Check if form was submitted
if (isset($_POST['form_name']) {
// Form submitted
}
?>
<?php
// Check if radio was selected
if (isset($_POST['announcefiles']) {
// Form submitted
echo 'You chose' . $_POST['announcefiles'];
}
?>
Try this:
You may have an easier time if you separate the php and html a little more.
<form method="post" action="settings.php" onchange="this.form.submit()">
<fieldset>
<legend>Announce New Files:</legend>
<label for="on"><input type="radio" id="on" name="announcefiles" value="On" <?php echo $checkon1 ?> /> On</label>
<label for="off"><input type="radio" id="off" name="announcefiles" value="Off" <?php echo $checkoff1 ?> /> Off</label>
</fieldset>
</form>
Then in your php logic in settings.php ( or above the form if you are posting back to the same page ) you can check for the value of announcefiles:
<?php
if(isset($_POST['announcefiles'])){
// DO SOMETHING
}
?>
Let me know if this helps. Or if I'm missing the question.

How can I pass the value of a hidden text field to another php page?

I have a php page with the following:
echo "<form method='post'><input type='hidden' id='memberIdd' name='memberIdd' value=" . $memberId . "></form>";
Now I created another php page and want to grab the value of this hidden field and place it into another variable. How can I do this? I tried using this for my second php page:
$member = $_POST['memberIdd'];
But I just keep getting "undefined" for $member.
Thanks
I think your problem is that you did not set the right action in the form and you also should have a submit button. Thus, your form should look like:
echo '<form action="url-to-the-second-page.php" method="post">';
echo '<input type="hidden" id="memberIdd" name="memberIdd" value="' . $memberId . '">';
echo '<input type="submit" name="submit" value="submit" />';
echo '</form>';
<form method='post' action='secondpage.php'>
http://www.w3.org/TR/html4/interact/forms.html#h-17.1
If you don't write the action then the posted data goes to the same page where you were.
And by the way, the variable $memberId (the one in your question) at the firstpage.php should be defined. In other case it will prompt error message.
Edit: It will better for you to use HTML codes out of PHP.
That's a hidden form field, but you've no submit button. The value won't appear in the second page unless you specify it as the form's action (i.e. where the form should be submitted to), and also submit the form. e.g.
echo "<form action='page2.php' method='post'>
<input type='hidden' id='memberIdd' name='memberIdd' value=" . $memberId . ">
<input type="submit" value="Go to page 2">
</form>";
Alternatively, you could store the value as a session variable.

Categories