Hi I'm trying to create a small program to
change a string based on a HTML radio input,
Think I've got the basics of it complete
but not sure I'm doing it the correct way,
I'm trying to use a PHP switch but do not know which part I am supposed to attach the switch to. Currently its trying to grab the fieldset, and switch based on the radio options set within that switch.
my PHP code is below:
<!doctype html>
<html>
<head>
<title>String Alteration</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
<link rel="preconnect" href="https://bit.ly"/>
<link rel="preconnect" href="https://port-80-ywfrg4rd7y.treehouse-app.com"/>
</head>
<body>
<form class="string_form" width="720" height="120">
<div class="span_container">
<span class="numbers_span">
Please input a String<br>
into the text field Below.
</span>
</div>
<div class="psuedo_input_outter">
<input name="InitialString" type="text"
minlength="2" maxlength="255" value="" required>
</div>
<fieldset name="StringOperator">
<label for="Option1_TransformUpperCase"
class="Numbers_label">Transform To<br>Upper Case</label>
<input type="radio" name="rad0" value="UpperCase"><br>
<label for="Option2_TransformLowerCase"
class="Numbers_label">Transform To<br>Lower Case</label>
<input type="radio" name="rad1" value="LowerCase"><br>
<label for="Option3_FirstCharUpperCase"
class="Numbers_label">First Charater<br>Upper Case</label>
<input type="radio" name="rad2" value="FirstChar"><br>
<label for="Option4_AllWordsFirstUpperCase"
class="Numbers_label">First Charater<br>All words Upper Case</label>
<input type="radio" name="rad3" value="AllFirstChar"><br>
</fieldset>
<button type="submit" id="No_Submit"
name="Submit" class="number_challenge_submit">Submit</button>
</form>
<?php
error_reporting(E_ALL);
session_start();
$_initialString;
$_outputString;
// ::>> Notes: !!>>> Why does HTML attribute checked="true" not move onto the new radio when a different one is selected?
// ::>> Notes:
if(isset($_GET['Submit'])){
if(isset($_GET['InitialString'])){
$_initialString = $_GET['InitialString'];
$ToUpperCase = false;
$ToLowerCase = false;
$FirstCharUpper = false;
$AllFirstUppper = false;
$stringOperator = isset($_GET['StringOperator']);
switch($stringOperator){
case "rad0": // Not sure these are correct check them on SO, w3school..
$ToUpperCase = true;
$newStr = strtoupper($_initialString);
echo "<span class='result'>" . $newStr . "</span>";
break;
case "rad1":
$ToLowerCase = true;
$newStr = strtolower($_initialString);
echo "<span class='result'>" . $newStr . "</span>";
break;
case "rad2":
$FirstCharUpper = true;
break;
case "rad3":
$AllFirstUpper = true;
echo ucwords($_initialString);
break;
}
// Need to create a check to see which radio button is set.
// Atleast one of the radio buttons needs to have the html attrib required
// Use javascript to move this attrib around, based on user select then add attrib
// disabled to all others
// substr_replace($_initialString);
// Need strtoupper();
// Need strtolower();
// Need ucwords();
// Need lcfirst();
// Need to bind these to the radio buttons and use
// javascript to disable any none selected radio's
} // 2nd if();
} // 1st if();
?>
<script src="/Js/numbersJs.js" charset="utf-8"></script>
</body>
</html>
JS
console.log('numbers linked ok..');
document.addEventListener("DOMContentLoaded",function(e){
let qSAll =(s)=>{
return document.querySelectorAll(s);
}
let radioBtns = qSAll('input[type=radio]');
if(radioBtns){
// ::>> console.trace(radioBtns);
for(var i=0;i<radioBtns.length;i++){
if(radioBtns[i].checked == true){
console.log(radioBtns[i].value);
}
}
}
console.log(radioBtns);
//if(radioBtns[0]){
// console.log('checked');
//}
});
CSS
.string_form{
overflow:hidden;
/* min-width:calc(748px + 2.2vw); */
/* max-height:calc(438px + 0.85vh); */
}
.span_container{
background:linear-gradient(-33deg,#cdcecc,white,gold);
border-radius:4px;
user-select:none;
}
.numbers_span{
font-size:calc(12.2px + 1.23vw + 0.234vh);
padding:1px 18px 1px 18px;
font-family:'Knights Templar', sans-serif;
border-radius:4px;
}
input[type=text]{
position:relative;
width:411px; max-width:411px;
transform:translateX(-52px);
padding:0 2px 0 2px;
border-radius:4.8px;
}
input[type=radio]{
position:relative;
margin-bottom:calc(28px + 0.08vh);
max-width:14px;max-height:14px;
transform:translateX(-174px) translateY(25.8px);
}
.Numbers_label{
clear:both;
float:left;
background:linear-gradient(-33deg,rgba(242,213,162,0.7),gold);
padding:4px 9.2px 4px 9.2px;
font-family:'Knights Templar',sans-serif;
user-select:none;
border-radius:9px;
border-bottom-right-radius:2.1px;
margin-bottom:4px;
}
.numbers_label::before{
display:block;
position:relative;
left:-42px;
width:25px; height:25px;
background:orange;
}
.result{
position:absolute;
z-index:555;
top:65%; left:50%;
transform:translate(-50%,-50%);
padding:8.2px;
background:linear-gradient(-32deg,green,gold,orange);
}
Also want the javascript to disable all other radio buttons when one is selected but can't seem to figure that part out, can anyone give me some pointers so I'm heading in the right direction..
All the answers I have looked at for disableing radio inputs are using JQuery, and when I have tried to translate that inot vanilla javascript it ends up not working..
Just noticed a mistake thatt I've made none of the labels creespond to any Ids, so now changing that part out.. But can anyone give me any php pointers ...
in my javascript code i have got the following script so far, attempting to add checked in the HTML DOM to the selected radio & remove the checked attribute for any radio's that are not the current selected item.
radioBtns = qSAll('input[type=radio]');
if(radioBtns){
let Btn;
// ::>> console.trace(radioBtns);
for(var i=0;i<radioBtns.length;i++){
radioBtns.forEach(function(){
this.addEventListener("click", function(e){
// Change e.target to checked.
e.target.setAttribute("checked", "true");
// checked="false" 4 all others..
let prevRad = e.target.previousElementSibling; // [contains(@class, 'rad')];
console.log(prevRad);
prevRad.removeAttribute("checked");
})
}
}
the problem with this is that e.target.previousSibling &or nextSibling returns the html element<br>
if i try to use previousElementSibling it is returning the label. not the next radio input up or down in the list..
is there an easier way to de-select the not current/active/used radios & why is this not the default behaviour in the devtools console.. radio stays checked in the markup when selecting another radio even if visually on the screen it is not selected...
Your syntax is wrong here. First of all you should set the name of each radio button same and the value should be used in switch case like UpperCase, LowerCase.
You are actually using the name of the radio button as switch case thats why the click on the radio doesn't work
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.