Using JavaScript to Validate Radio Buttons [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i'm trying to follow this tutorial guide on this website http://homepage.ntlworld.com/kayseycarvey/jss3p11.html?
However, i met some difficultly when doing so.
Even though i did the exact same thing. Please guide me if you know so.
Here are what i did:
<html>
<script>
function GetSelectedItem() {
chosen = ""
len = document.f1.r1.length
for (i = 0; i <len; i++) {
if (document.f1.r1[i].checked) {
chosen = document.f1.r1[i].value
}
}
if (chosen == "") {
alert("No Location Chosen")
}
else {
alert(chosen)
}
}
</script>
<body>
<form name="f1">
<Input type = radio Name = r1 Value = "NE">North East
<Input type = radio Name = r1 Value = "NW">North West
<Input type = radio Name = r1 Value = "SE">South East
<Input type = radio Name = r1 Value = "SW">South West
<Input type = radio Name = r1 Value = "midlands">Midlands
</form>
</body></script>
</html>
On a side note, do i have to do anything to the form in order to trigger the function GetSelectedItem ?
Thanks in advance !

add GetSelectedItem ()function on onchange event of your radio button
<input type="radio" onclick="GetSelectedItem()" value="NE" name="r1">

Add onsubmit="javascript:GetSelectedItem();" to your form tag.

window.addEventListener('load', function () {
for (var i = 0; i < document.f1.r1.length; i++) {
document.f1.r1[i].addEventListener("click", GetSelectedItem);
}
}, false);
function GetSelectedItem() {
alert(this.value);
}
http://jsfiddle.net/udSbL/2/

try this
<input type="radio" onclick="autosubmit(this.value)" value="NE" name="r1">
<script type="text/javascript">
function autosubmit(value) {
window.location='update.php?radiovalue='+value;
}
</script>
update.php
$rbtn=$_GET['radiovalue'];
//here your update query

Related

How do I dynamically query the DB based on HTML select option? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a HTML drop-down list populated by querying a database. When the user chooses an option, the application should show information related to that option, with no submit!! That is, if I choose option A, automatically the application should query the DB for that info and display it.
The part of querying and displaying the data is not a problem, as it's similar to what I did for populating the list, but what I don't know is how to do this dynamically. I know how to do it using submit, but I'm not allowed to do this way for a better user experience.
As an example, suppose a hotel booking application. There are two elements, the drop-down list with dates, and a message showing the number of bookings there are for that determined date. So, upon visiting the application it may show:
Date: 2015/11/26 Bookings: 7
But now if the user changes the date, it should display the number of bookings for that new date:
Date: 2015/11/27 Booking: 18
You're looking for AJAX calls.
An example of doing so with jQuery:
$("#id-of-your-select").change(function(){
var val = $(this).val();
$.post("php-page.php", { selectValue : val }, function(response){
console.log(response); //handle the data pinged back by the PHP page in the variable 'response'
});
});
In the PHP file php-page.php, you receive the data in $_POST["selectValue"], and whatever you output (via echo, print, print_r, var_dump, exit() etc) will be received by the JavaScript/jQuery function in the variable "response".
References:
jQuery Ajax
jQuery Post
Here is example of my project where i need to choose option from drop down list of Countries and based on that to query related data from database.
$('#ddl_country').change(function(){
var country = $('#ddl_country :selected').val();
$.ajax({
url: "/admin/findDivisions",
type: 'post',
dataType: 'json',
data: {country:country},
cache: false,
success: function(data){
$('#ddl_division').html('<option value="0"> - Select - </option>');
for (var i = data.length - 1; i >= 0; i--) {
$('#ddl_division').append('<option value="' + data[i]['id'] + '">' + data[i]['name'] + '</option>')
}
}
});
});
I've used ajax because i wanted to fill another drop down list with options generated from database without any reloads.
You looking for this:
HTML + jQuery
<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
$('#values_to_choose').change(function(){
document.getElementById("values_to_choose").value=this.options[this.selectedIndex].text;
});
$("#values_to_choose").change(function(){
var value_val = $(this).val();
$('#values_to_be_filled').empty();
$.post('get_values.php', {value_val:value_val}, function(result){
if (result != 1){
theResult = JSON.parse(result);
var theLength = theResult.length;
var x = document.getElementById("values_to_be_filled");
var option = document.createElement("option");
option.text = "Please select ...";
option.value ="";
x.add(option);
for(i=0;i<theLength;i++){
var new_data = theResult[i].split(',');
var result_val = theResult[i];
var x =document.getElementById("values_to_be_filled");
var option = document.createElement("option");
option.value = result_val;
option.text = result_val;
x.add(option);
}
}else if(result == 1){
alert("No value for selected option");
}
});
})
});
</script>
</head>
<body>
<form method="post" action="" name="user" id="user">
<input id="v1" type="hidden" name="v1"/>
<<select name="values_to_choose" id="values_to_choose">
<option value="" selected="selected">Please select ...</option>
<option value="1" selected="selected">Value 1</option>
<option value="2" selected="selected">Value 2</option>
<<select name="values_to_be_filled" id="values_to_be_filled">
<option value="">Please select ...</option>
</select>
</form>
</body>
</html>
PhP Script for data...
<?php
require_once('DB Connection');
if($_POST['value_val'] != ""){
$get_value = $_POST['value_val'];
}
$get_results = mysql_query("select values_for_chosen_option from table where column_name ='$get_value'") or die(mysql_error());
$get_rows = mysql_num_rows($get_results);
if ($get_rows > 0){
$check=0;
$items=array();
while ($row=mysql_fetch_assoc($get_results)){
$data = $row['values_for_chosen_option'];
array_push($items,$data);
}
}else{
$check=1;
}
if ($check==0){
echo json_encode($items);
}
else{
echo "1";
}
?>

Load second drop box with specific value from first drop box in HTML [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to create 2 drop box
when i select mobile form 1st drop box
2nd drop box should open mobile brand list
if i click Computer in first box
list of computer brand should open
use ajax, it could be something like this
YOUR HTML
<select id="combo1" name="combo1" onchange="getvaluesforcombo2($('#combo1').val());">
<option value=""></option>
<option value="1">Computer</option>
<option value="2">Mobile</option>
</select>
<select id="combo2" name="combo2">
<option value=""></option>
</select>
YOUR JS
function getvaluesforcombo2(valuec1) {
var postData = {combo1v:combo1v};
$.post('getvaluefor2.php', postData, function(data) {
var obj = $.parseJSON(data);
var sel = $("#combo2");
sel.empty();
sel.append('<option value=""></option>');
$.each(obj, function(k, v){
sel.append('<option value="'+v.Code+'">'+v.Name+'</option>');
});
});
}
AND YOUR PHP
if (isset($_POST['combo1'])) {
$combo1= $_POST['combo1'];
$query= $this->db_connection->prepare('SELECT type, name FROM mobileandpcbrands WHERE type = :combo1');
$query->bindValue(':combo1', $combo1, PDO::PARAM_INT);
$query_pilar->execute();
//if there is results
if ($query->rowCount() != 0) {
$result = $query->fetchAll();
foreach ($result as $results) {
$rows[] = $results;
}
echo json_encode($rows);
}
This is an example i hope this help you

need advice and bug help please! (HTML and JS, maybe PHP and MySQL) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make an auto complete forum (like the tags below) that works in lime-survey. I'm new to this so please explain everything like i'm 5 :)
My goals are:
auto complete to work
work with lime-survey
have an excel file or maybe a database using PHP/MySQL that anyone can manage by editing. It would only be ONE row. Please point me how to do this. I use a mac :)
Here is the code:
<!DOCTYPE HTML>
<html>
<body>
<div >
<input type="text" id="raw_input" width="100" height="30" name="box" onkeyup=show(this)>
</div>
<div id="drop_down" style="display:none">
<SELECT id=box size=3 name="box" onclick=show(this)></SELECT>
</div>
<script>
function drop_the_box() {
document.getElementById("drop_down").style.display = "none";
document.getElementById('box').length = 0;
if (!randomcharactersblablabla).test(document.getElementById("raw_input").value){
document.getElementById("drop_down").style.display="block";
var database = new Array("object_1","item_1","object_2","item_2");
var string = document.getElementById("raw_input").value;
for (var s = 0; s < database.length; s+= 1 ) {
var t += 1
if (database[s].indexOf(string) != 0) {
addItem(string[s],database[s]);
scan(streetArray[s],streetArray[s]);
}
}
}
}
function scan(x,y) {
var ghost_tag = document.createElement("ghost");
document.getElementById("box").options.add(ghost_tag);
ghost_tag.text = x;
ghost_tag.value = y;
}
function show(visable) {
document.getElementById("dropdown").value = visable.value;
}
</script>
</body>
</html>
Keep you data in mysql database. Create php file which will handle queries. Use jquery.ajax() to send queries and retrieve responses from php file.
Use this example
jQuery file
$('#search').change(function(){
var name = $('#search').val();
$.ajax({
type: 'POST',
url: 'request.php',
data: 'some data(may be variable)',
success: function(response){
$('#searach').val(response);
}
});
})
php file
if(isset($_POST['some_data'])){
$query = 'SELECT your_table_field FROM your_table WHERE your_table_field LIKE %$_POST['some_data']% LIMIT 1';
$result = mysql_query($query);
$myrow = mysql_fetch_array($result);
echo $myrow[0];
}

I need the first two letter of the first and the last name from text box to some where in a page [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need the first two letter of the first name and the last name which will type in text box like Salman Shaikh so it will cm some where in my page as SASH i need this.
Here is live example with jQuery.
http://jsfiddle.net/rPTrK/
I'll paste the code in here also -
jQuery(document).ready(function() {
jQuery("#submit").click(function() {
var firstName = $("#name").val().substr(0, 2);
var surname = $("#surname").val().substr(0, 2);
$("body").append(firstName+surname);
});
});​
In case you don't know - jQuery is JS library. You can get it here . Download the latest jquery release and insert it in your html/php file between the tags, just like you normally insert js scripts.
Attention: You must insert my created code only after you have inserted the jQuery library, otherwise it won't work.
EDIT: Updated to your suggested version -
http://jsfiddle.net/pg8La/
Code -
jQuery(document).ready(function() {
jQuery("#form1").change(function() {
var firstName = $("#name").val().substr(0, 2);
var surname = $("#surname").val().substr(0, 2);
if(firstName != "" && surname != "") {
$("body").append(firstName+surname);
}
});
});​
EDIT 2: Final example per your request -
http://jsfiddle.net/K52fR/
And code here -
HTML
<form id="form1">
<input type="text" name="name" id="name" placeholder="name" />
<br />
<input type="text" name="surname" id="surname" placeholder="surname" />
<br />
<h2 id="text">WSCPP<span id="nameSurname"></span><span id="time"></span><span id="randomNr"></span></h2>
</form>​
jQuery/JS -
jQuery(document).ready(function() {
jQuery("#form1").change(function() {
var firstName = $("#name").val().substr(0, 2);
var surname = $("#surname").val().substr(0, 2);
if(firstName != "" && surname != "") {
$("#nameSurname").text(firstName+surname);
}
});
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate(); var year = currentTime.getFullYear();
$("#time").text(day + "" + month + "" +year);
$("#randomNr").text(Math.floor(Math.random()*90000+9999));
});​
I assume that you are POST-ing the data to some page which is why you have a tag php
in your question.
$firstname = substr($_POST['firstname'],2);
$lastname = substr($_POST['lastname'],2);
Check following for program
http://jsfiddle.net/YNV87/
How about this:
$_REQUEST['firstname'] = 'Salman ';
$_REQUEST['lastname'] = 'Shaikh';
$firstname = strtoupper(substr($_GET['firstname'], 0, 2));
$lastname = strtoupper(substr($_GET['lastname'], 0, 2));
echo $firstname.$lastname;
Explode the string to an array.
$name_array=explode(' ',$name);
$first=substr(trim($name_array[0]),0,2);
$last=substr(trim($name_array[1]),0,2);

I figure it out myself [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
in the updates.. with my code above, it is working if i only choose 1 row but when i have chosen more than 1 row, the value in the last row that i inserted is the only value that mysql is using to updates all rows. 2 yes it is intended. i need it for my updates, 3rd sorry, but i am using CSS though i will clean my code after i finish my program, im just stack here
This is my parent window HTML code
JAVASCRIPT
function MM_openBrWindow(theURL,winName,features) { //v2.0
var chkValue = "";
var counter = "";
for (var i=0; i < document.myForm.chkbox.length; i++)
{
if (document.myForm.chkbox[i].checked)
{
chkValue = chkValue + document.myForm.chkbox[i].value + " ";
}
counter ++;
}
var queryString = "&chkValue=" + chkValue;
// location = "featuredaction.php" + queryString
//var queryString = "id=" + id;
var theURL = theURL + queryString;
//var tbreceiptno= document.getElementById('checkbox').value;
window.open(theURL,winName,features);
}
I think i will fgure it out my own thanks everyone..
You have fault in your second line $chkValueArr=explode(" ", $chkValue);. You dont have to explode it. You already got an Array of checkbox value when you wrote $chkValue=$_GET['chkValue']; Assuming your HTML form is something like:
<input type="checkbox" name="chkValue[]" value="val 1">
<input type="checkbox" name="chkValue[]" value="val 2">
<input type="checkbox" name="chkValue[]" value="val 3">
You can loop over all checkbox values by:
foreach($chkValue as $chk)
{
echo $chk;
}
This will print val 1, val 3 if you selected those checkboxes in your form.
You can check the array by writing print_r($chkValue)

Categories