I have a code here...the thing that i want is to calculate the total instantly and show in the textbox without clicking the action button.
code:
<?php
include('include/connect.php');
if(isset($_POST['enter']))
{
$name = $_POST['name'];
$score1 = $_POST['optA'];
$score2 = $_POST['optB'];
$score3 = $_POST['optC'];
$score4 = $_POST['optD'];
$total1 = $_POST['total'];
$total = ($score1 + $score2 + $score3 + $score4);
mysql_query("INSERT INTO score (name,score1,score2,score3,score4,total) VALUE ('$name','$score1','$score2','$score3','$score4','$total')");
echo "succesful";
}
else
{
echo "you fail to execute!";
}
?>
code:
<form method="post" action="index.php">
<input type="text" name="name" />
<input type="radio" name="optA" value="1" />1
<input type="radio" name="optA" value="2" />2
<input type="radio" name="optA" value="3" />3<br>
<input type="radio" name="optB" value="1" />1
<input type="radio" name="optB" value="2" />2
<input type="radio" name="optB" value="3" />3<br>
<input type="radio" name="optC" value="1" />1
<input type="radio" name="optC" value="2" />2
<input type="radio" name="optC" value="3" />3<br>
<input type="radio" name="optD" value="1" />1
<input type="radio" name="optD" value="2" />2
<input type="radio" name="optD" value="3" />3
<input type="text" name="total" value="<?php echo $total ?>" />
<input type="submit" value="enter" name="enter" />
</form>
PHP is executed before the content reaches the browser, so no elements will be loaded, so you cannot access them. It is a server-side language, not client-side; therefore you cannot do this. An example of something client-side is Javascript. However, you can create a HTML element and set its attributes, then echo it onto the page.
You'll have to use JavaScript in some form or another to not have to post the answer back to the server and wait for a response.
Here's how you can do it in JQuery.
function calculateTotal() {
var total = 0;
$('form input:radio:checked').each(function () {
total = total + parseInt($(this).val());
});
$('form [name=total]').val(total);
return false;
}
In your HTML you'll need to change your submit button to something else like this:
<span onclick="calculateTotal()">Enter</span>
Related
I got a list of 20 questions with A or B answer, and for the result I need to count certain answers together to get a score, and its A and B mixed.
Question 1 <input type="radio" name="1" value="A" />
<input type="radio" name="1" value="B" />
Question 2 <input type="radio" name="2" value="A" />
<input type="radio" name="2" value="B" />
Question 3 <input type="radio" name="3" value="A" />
<input type="radio" name="3" value="B" />
$result1 = $1A + $2B + $4A + $7B etc (count total of these answers)
$result2 = $3B + $5B + $6B etc
the results should be numeric, with checkboxes it would be easy because of unique names so values can be 1, but with radios I don't know how other then a lot of 'if 1=B then $var++', but i'm sure there is an easier way.
Here you go:
<?php
var_dump($_POST);
/*
#param array $userInput - just the $_POST
#param array $expected must be in format : [
[1,'A'],
[2,'B']
]
#return int
*/
function countExpectedAnswers(array $userInput, array $expected) /*: int*/{
$result = 0;
foreach($expected as $ex){
if(isset ($userInput[$ex[0]]) && $userInput[$ex[0]] === $ex[1]){
$result ++;
}
}
return $result;
}
$expectedAnswers = [
[1,'A'],
[2,'A'],
[3,'B']
];
echo 'Answered correctly: ' . countExpectedAnswers($_POST,$expectedAnswers);
?>
<form method = 'post'>
<div>
Question 1 <input type="radio" name="1" value="A" >
<input type="radio" name="1" value="B" >
</div>
<div>
Question 2 <input type="radio" name="2" value="A" >
<input type="radio" name="2" value="B" >
</div>
<div>
Question 3 <input type="radio" name="3" value="A" >
<input type="radio" name="3" value="B" >
</div>
<input type = 'submit' value = 'go'>
</form>
I have sucessfully sent my data to my page in the form of the url :
http://localhost:8101/Tutorials/sandbox/myfiles/make_a_booking.php?period=2&date=04/29/2014&room=028
But I am struggling to place the data into the form. But how would I place the period=2 into the radio buttons and the room=028 into the select drop down?
<form class="submit_date" method="post" action="insert.php" id="submit_date" onsubmit="return confirm('Confirm your booking?');">
<p>Date: <input type="text" name="datepicker" id="datepicker" required value="<?php echo $_GET['date'];?>"></p>
<input type="radio" name="bookperiod" id="bookperiod" value="1" required/>1<br>
<input type="radio" name="bookperiod" id="bookperiod" value="2" required/>2<br>
<input type="radio" name="bookperiod" id="bookperiod" value="3" required/>3<br>
<input type="radio" name="bookperiod" id="bookperiod" value="4" required/>4<br>
<input type="radio" name="bookperiod" id="bookperiod" value="5" required/>5<br>
<select class="dropdown" id="bookroom" name="bookroom" required;>
<option selected disabled hidden value=''></option>
<?php
for ($x=0; $x<sizeof($rooms_array); $x++)
{
echo "<option value='$rooms_array[$x]'>".$rooms_array[$x]."</option>";
}
?>
</select>
<input class="submit_btn" value="Submit" type="submit" name="Submit";/>
</form>
I've tried placing the room=028 into option via $_GET but it hasnt worked and the radio buttons I wouldnt know where to begin.
you can use php for that:
<?php
$selectedPerion = $_GET['period'];
for($x = 1; $x < 6; $x++): // short notation to separate php and html better?>
<input type="radio" name="bookperiod" id="bookperiod" value="<?php echo $x ?>" required
<?php echo ($selectedPerion == $x ? 'checked' : ''); //so called 'ternary operator' ?>/>
<?php echo $x ?>
<br>
<?php endfor; ?>
Or you can use jQuery to set the 'checked' property or simulate click on the proper button, but that's another story.
it is similar for the select dropdown, you have to add the 'selected' property to proper "" tag, or set the value for bookroom field with jQuery.
So basically, my loop outputs four times the html below with different values in $show_id.
Everyform returns the $show_id value of the first form instead of the value of the form when the function is called. How do I fix that ?
my javascript
<script type="text/javascript" >
function addScore() {
var show_id = $('#show_id').val();
var user_id = $('#user_id').val();
var score = $('input[name=tvshowrating]:checked').val();
if(score=='')
{
alert('PleaseEnter A Score');
}
else
{
$("#flash").show();
$("#flash").html('<img src="ajax-loader.gif" />Loading Score...');
$.ajax({
type: "POST",
url: "showscoreajax.php",
data:{
"show_id" : show_id,
"user_id" : user_id,
"score" : score //we are passing the name value in URL
},
cache: false,
success: function(data){
$("#flash").html('Added');
}
});
}
return false;
};
</script>
myhtml
<div id="flash"></div>
<form id="form3B">
<div class="your-score">
<div class="">Your Score</div>
<input class="hover-star" type="radio" name="tvshowrating" value="1" title="1"/>
<input class="hover-star" type="radio" name="tvshowrating" value="2" title="2"/>
<input class="hover-star" type="radio" name="tvshowrating" value="3" title="3"/>
<input class="hover-star" type="radio" name="tvshowrating" value="4" title="4"/>
<input class="hover-star" type="radio" name="tvshowrating" value="5" title="5"/>
<input class="hover-star" type="radio" name="tvshowrating" value="6" title="6"/>
<input class="hover-star" type="radio" name="tvshowrating" value="7" title="7"/>
<input class="hover-star" type="radio" name="tvshowrating" value="8" title="8"/>
<input class="hover-star" type="radio" name="tvshowrating" value="9" title="9"/>
<input class="hover-star" type="radio" name="tvshowrating" value="10" title="10"/>
<input type="hidden" id="show_id" value="<?php echo $row[0]; ?>" />
<input type="hidden" id="user_id" value="<?php echo $user_id ?>" />
<span id="hover-test" style="margin:0 0 0 20px;"></span>
<input id="submitscore" type="submit" value="Submit scores!" onclick="addScore()" />
</div>
</form>
</div>
</div>
This is because you cannot have duplicate id's unlike duplicate class you need different ids for each form. something like this
<input type="hidden" id="show_id-form3B" value="<?php echo $row[0]; ?>" />
Then it will be possible for you to get unique value of each different forms. Some thing like this
function addScore(formId) {
var show_id = $('#show_id-'+formId).val();
var user_id = $('#user_id-'+formId).val();
//add your code
}
I need a form in which the checkboxes would open different pages based on their selection when the form is submitted.
So, say I have this simple form:
<form action="" method="post">
<input value="1" type="checkbox" name="sign" />
<input value="2" type="checkbox" name="sign" />
<input value="3" type="checkbox" name="sign" />
<input value="4" type="checkbox" name="sign" />
<input value="5" type="checkbox" name="sign" />
<input type="submit" />
</form>
When an user checks value 1 and submits, he will be redirected to page A. If a user checks 1 and 4 he will be redirected to a different page (page F, for instance). If a user checks 2, 3 and 4 he will be redirected to page R, and so on... There would be 25 different combinations, and therefore, 25 different page results for this form when an user submits it.
In other words, when the form is submitted somehow the system would read which checkboxes were checked and associate each possible combination with a different URL.
Can it be done? If so, how? Anyone ever made something similar? I've searched a long time for solutions, but found only slightly similar ones, not exactly what I need, so any help would be appreciated.
HTML:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[1]" />
<input value="2" type="checkbox" name="sign[2]" />
<input value="3" type="checkbox" name="sign[3]" />
<input value="4" type="checkbox" name="sign[4]" />
<input value="5" type="checkbox" name="sign[5]" />
<input type="submit" />
</form>
PHP:
if (isset($_POST['sign'][1]))
header("Location: a.php");
elseif(isset($_POST['sign'][2]) AND isset($_POST['sign'][3]))
header("Location: b.php");
This can be done in a couple of ways. One is that you can use javascript to intercept the form submission, change the value of the form "action", and then execute the submit.
A second approach might be to just send all this data to a single script and based on the selected values perform a redirect to the intended page.
From should look like:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
And the post like:
if(!empty($_POST['sign'])) {
if(in_array(1, $_POST['sign'])) {
// if just 1, go to page
} elseif(in_array(1, $_POST['sign']) && in_array(4, $_POST['sign'])) {
// if 1 and 4, go to page
}
}
You can just consider each of the inputs as a bit. So, you will always have 5 bits to consider.
Then define an associative array of 25 entries corresponding to each of the possible values:
00001
00010
...
var links = {
"00001" : "www.google.com",
];
Then, when you submit the form, just set the target attribute of the form based on the value.
If I were you and I had no choice than following this idea, I would use something like that in jQuery (assuming your post vars are treated in the page you want to reach):
<form action="" method="post" id="myForm">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
$("#myForm").submit(function() {
var checked_array = new Array();
$("#myForm input").each(function() {
if ($(this).is(":checked")
checked_array.push($(this).attr("value"));
});
if ( checked_array.indexOf(2) !== -1 && checked_array.indexOf(5) !== -1)
("#myForm").attr("action", "/url1.php") ;
else if etc...
});
I have multiple checkboxes in my form and i want to use those to generate the url.
An example :
<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox" value="a">
<input type="checkbox" name="checkbox" value="b">
<input type="checkbox" name="checkbox" value="c">
<input type="checkbox" name="checkbox" value="d">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
When the user presses the submit button he will have to be redirect to the generated url created as followed :
www.domain.com/checkbox/a+b+c+d
Depending on the choises of the user the url should be generated as above.
How can i pull this of?
In your checkbox.php, get all the checked checkboxes, create the url you want, and redirect the user to it with header("Location: $url").
HTML:
<input type="checkbox" name="checkbox[]" value="a" />
<input type="checkbox" name="checkbox[]" value="b" />
<input type="checkbox" name="checkbox[]" value="c" />
<input type="checkbox" name="checkbox[]" value="d" />
checkbox.php:
<?php
if ($_POST) {
$url = "http://www.domain.com/checkbox/";
$params = '';
$checkBoxes = $_POST['checkbox'];
foreach ($checkBoxes as $value) {
$params .= $value;
if ($value != $checkBoxes[count(checkBoxes) - 1])
$params .= '+';
}
$url .= $params;
}
header("Location: $url");
Here's a php example if you don't want to use AJAX
Top of your checkbox.php:
<?php
if (isset($_POST['checkbox'])){
$url = 'checkbox/' . implode('+',$_POST['checkbox']);
header('Location:'. $url);
die;
}
?>