How do you output text of a selected radio button?
For example:
<form method="POST" action="form.php">
<input type="radio" name="color" value="red">My favourite color is red.
<input type="radio" name="color" value="blue">My favourite color is blue.
</form>
<span id="feedback"></span>
So, if I click the button with a value of red. How can I output the text "My favourite color is red." and have the text inside the span?
I only know how to echo the value, not the text.
Would appreciate an example!
Edited.
Once you submit a form, you will only get what the page is being sent through the form. There is no way of getting the exact text next to a radio button that isn't sent along with the form, but you can simply take the value and construct the text again.
If you are sending the form by POST, you could do the following:
if (isset($_POST['color'])) {
echo 'My favorite color is '.htmlentities($_POST['color']);
}
Note the use of htmlentities() – never trust user input. This function replaces characters like < to their corresponding HTML entity, e.g. <. This way, a malicious user can't send some HTML tags as value and break your page.
If you want to send by GET, replace $_POST['color'] with $_GET['color'].
You will need to send the text value with some help of JS to PHP. Should you look at the request details you will see only name and value are sent to PHP.
Related
I'm building an advanced search with a-tags to filter for first letter abcdefghijklmnopqrstuvwxyz0-9 and also an input field, options fields for other criteria, radio buttons and a submit button. When I click on a letter-filter, it sends it as $_GET variable. But when I then use the input fields it forgets it of course. How can I grab this variable with the submit button before sending it along with all the other variables from the form? Do I have to use JS and AJAX for that? Do you recommend that?
EDIT
so let me rephrase that.
I have different variables to send to the server, coming from input fields, radio buttons, option fields but I also want a link for each letter of the alphabet (so it would then list all entries that start with that letter).
When I click the submit button it sends all the variables from the input fields but not from the a-tags (cause they are not recognized as form-fields I guess). When I click on the a-tags (a letter) it sends just that very variable to the server which is fine as well. My question is, how can I get the submit button to send everything?
<input type='search' id='search-query' name='q' class='uk-input uk-form-width-medium' placeholder='Suche'>
<p>
<?php
foreach ($alphabet as $abc){
if ($abc == $letter) {
$class = 'current';
}
else $class = '';
echo '<a class="'.$class.'" href="'.$page->url.'?letter='.$abc.'">'.$abc.'</a> ';
}
?>
</p>
<select class="uk-select uk-width-medium" name="what">
<option>Titel</option>
<option>Autor</option>
<option>Erscheinungsdatum</option>
</select>
<label><input class="uk-radio" type="radio" name="how" value="aufsteigend" checked> aufsteigend</label>
<label><input class="uk-radio" type="radio" name="how" value="absteigend"> absteigend</label>
<button class="uk-button uk-button-primary" type="submit">Suche starten</button>
</form>```
I assume you want to have dynamic Google-like search where you type in "a" and it gives you a drop-down list of options like "airplane, applle, artery" etc.
If that is the case, then yes, you do need JS + AJAX.
Instead of clicking on a letter filter, you can have AJAX dynamically display you the options available right in the input field.
Given the vague description of your question and the absence of images of your UI that is all that can be said.
I didn't really get the question and the whole situation from the explanation, but sounds like following:
You want to render the hints based on the value of the input field search-query when user clicks on the specific button: letter-filter. If this is right, then you will have to anyways send a get request to the back end so that it would give back the page with all hints.
FE:
<html>
<body>
<form action="app.com/myurl" method="GET" >
<input id="letter" value="a" name="letter" />
<input id="letter2" value="b" name="other_param" />
<input id="letter3" value="c" name="another_param" />
<!-- All three inputs will be visible in the $_GET variable when you send the request -->
<!-- Other input fields here: <input type="whatever" id="whatever".../> -->
<button type="submit">letter filter</button>
</form>
</body>
</html>
You can even track the request in borwser's network tab, if the params are not appended as query params like app.com/myurl?letter=a&other_param=b&another_param=c, if you dont see the query params but see the url itself, then I can suggest you forgot to set the method of the form.
But I am still not sure, can you rephrase your question, as it doesn't clearly explain the case, sorry your english is a little confusing.
EDIT AFTER Rephrase:
I understood your aim now and you are right, submit button sends only form fields and <a> tag is not a form field.
If you want to append all form fields to <a> tag click then you have few options.
Append all parameters on server side manually on render:
You have
foreach ($alphabet as $abc){
if ($abc == $letter) {
$class = 'current';
}
else $class = '';
echo '<a class="'.$class.'" href="'.$page->url.'?letter='.$abc.'">'.$abc.'</a> ';
}
And you need to additionally add form fields as well, I assume on first time click when none of the tags are rendered (no <a> tag on the screen) then you just send the form data, in tha moment you catch form data from $_GET and append to anchor
foreach ($alphabet as $abc){
if ($abc == $letter) {
$class = 'current';
}
else $class = '';
echo '<a class="'.$class.'" href="'.$page->url.'?letter='.$abc.'otherParamName='.$_GET['otherParam']].'">'.$abc.'</a> ';
}
You can use js, to collect form data, I guess you use jQuery, and append to the href on click of the anchor
jQuery('a.someClassIdentifier').click(fuction(e) {
// Please read documentation of jquery how to handle click events, I dont remember it out of my head right now.
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
But as an ultimate answer I would say rethink your approach. If you have already a form, and it sends GET request to the endpoint and it returns the same page where tags are visible then you can use form fields instead of anchor tags, that would already handle the data sending for you. But if you have different endpoints for tags receiving then best will be to send ajax request to receive only json data and render them without refreshing the page.
Hope it helps.
I am new to PHP and hope someone can help me with this.
I have an HTML form with a number of inputs and textareas. On Submit I pass the form to another PHP page that generates an email with its values.
For text inputs and textareas everything works as intended but for the radio buttons I can't get it to show me the value on the targetted page.
The radio buttons look as follows and the classes used there are only to apply some CSS.
There is only one form on the submitting page, all radios have the same name ("requestType") and there are no other elements using this name.
Also, I added a quick JavaScript snippet for testing to alert their value on change and this worked as well, so the issue seems to be only with the $_POST.
Can someone tell me what I am doing wrong here or what alternatives I could try here ?
My HTML (on the submitting page):
<input type="radio" class="customRadio radioDefault" id="requestType1" name="requestType" value="Value1" />
<label for="requestType1">Value1</label>
<input type="radio" class="customRadio triggerDiv" id="requestType2" name="requestType" value="Value2" />
<label for="requestType2">Value2</label>
My PHP (on the targetted page):
$_POST["requestType"]
Update:
As per RiggsFolly I tried to check whether it recognises that a radio button is checked which it seems it does, it just doesn't return anything as the following just returns "xx":
if(isset($_POST['requestType'])){
$theSelectedOne = $_POST['requestType'];
echo "radio value: x" . $theSelectedOne . "x";
}else{
echo "boohoo";
}
Radio buttons (and Checkboxes) are only passed back to the form in either the $_POST or $_GET arrays if they are actually checked. I notice you do not auto check one of them as you create the HTML so nothing is likely to be returned if the user makes no selection.
So the best way to test if they were checked or not is to test for the existance of their name in the $_POST array
if ( isset( $_POST['requestType'] ) ) {
// a selection was made
// so now test the value to see which one was checked
$theSelectedOne = $_POST['requestType'];
}
I want to display the input from a input-box text, right beside the input-box if it got data.
I want to use it in our checkout for the customer name.
That when the customer has entered their name, it echo beside it "Hi (customer-name)"
The input-box is on the same page. So the echo needs to be displayed right beside the input-box.
The data needs to be displayed when the customer is not focussing anymore on this input-box. So when it clicks on an other input-box or beside it, it needs to be displayed.
How can I fix that?
If you are OK with pure inline javascript, you can attain this using onblur event:
<input type="text" value="" onblur="if(this.value !=''){document.getElementById('hi').innerHTML='Hi, '+this.value;}else{document.getElementById('hi').innerHTML='';}" />
<span id='hi'></span>
Empty span, or whatever other html place holder will fill with "Hi, (username)" if the input loses focus and is not empty.
You are looking for Javascript, rather than php.
To make it very simple:
function sayHi(){
var name = document.getElementById("box1").value;
document.getElementById("username").innerHTML = name;
}
<input type="text" id="box1" onblur="sayHi();"><span id="username"></span>
I want to have a multi-step form with HTML and PHP.
The first step of my form is an option like:
<input type="radio" name="service_type" value="plan1"> Plan 1<br />
<input type="radio" name="service_type" value="plan2"> Plan 2
Now, my question is: how can I know which option is selected so that I arrange the next step options for the user?
For example: If the user chooses option 1, next step would be: "You have chosen option 1, tell me who's your daddy". And if the user chooses option 2, next step says: "Welcome to option 2, tell me what you like", etc.
Now, I'm a totally beginner in PHP/HTML and know nothing about javascript. If you're answering this, I'd be so thankful, but please do it in an easy-to-understand sort of way.
I have already found this related to my case, but it is very hard to customize, and the validation process is of before CSS3.
[edit:]
Now I want to add a text-type input like this:
<input type="text" name="fname" value="firstname">
The guys told me to use $_POST['fname'] but for input texts, the 'value' property will show up inside the textbox like a default caption. I don't want this.
Now what do you suggest?
the the value from $_REQUEST:
$step = $_REQUEST['service_type']; // plan1 or plan2
In your PHP code, use the $_GET (or $_POST or `$_REQUEST - which gets either a GET or POST form) to return the value:
$serveiceType=$_REQUEST['service_type'];
As this is a radio button, only one value can be sent, and the sent value is easily accessible.
At first your input must be in a form tag. Now you can submit the form with an submit button(Input tag with type="submit").
In php you get the results with $_POST or $_GET.
<form method="POST">
<input type="radio" name="service_type" value="plan1"> Plan 1<br />
<input type="radio" name="service_type" value="plan2"> Plan 2
<input type="submit" />
</form>
<?php
$value = $_POST['service_type'];
echo $value;
?>
I have this simple form:
<form method="post" action="?step=2">
<label>A4 - Colour / Colour
<input type="radio" name="leaflet" value="1"></label><br>
<label>A5 - Colour / Black
<input type="radio" name="leaflet" value="2"></label><br>
<input type="submit" name="leaflet" value="Select">
</form>
When I apply print_r ($_POST); to the submission though, I only get the submit button data. I don't even see the radio name.
What could do that?
PHP's standard form parsing system (which populates $_POST) can't handle multiple bits of form data with the same name (unless that name ends with the characters []).
Change the name of the submit button.
Buddy you have same name for submit button and radio button :)
ie leaflet
The value get overridden. I HOPE u got it