If radio button checked, then submit to different page - php

I have this:
<form method="post" id="kl" action="step2.php">
<input type="radio" name="rubrik" value="bussines"></input>
<input type="radio" name"rubrik" value="private"></input>
<input type="image" value="submit" src="/images/submit.png" alt="Submit" />
</form>
What i bassicaly want is: When the second radio button is checked, to submit the form to step2a.php, a different file. How can i do this? Jquery, Javascript, php?

You could do this with JavaScript (bind a submit listener that checks the value of the radio button and then sets the action property of the form), but it would be simpler and more reliable to do something (server side) along the lines of:
<form ... action="step-selector.php">
and
<?php
if (isset($_POST['rubrik']) && $_POST['rubrik'] == 'bussines') {
include('step2.php');
} elseif (isset($_POST['rubrik']) && $_POST['rubrik'] == 'private') {
include('step2a.php');
} else {
include('error-state.php');
}
?>

you can do this by modifying the Form into:
<form method="post" id="kl" action="step2.php">
<input type="radio" class="radio" rel="step2.php" name="rubrik" value="bussines"></input>
<input type="radio" class="radio" rel="step2a.php" name"rubrik" value="private"></input>
<input type="image" value="submit" src="/images/submit.png" alt="Submit" />
</form>
I added rel attribute to radio buttons. each has a value of the url. I also added a class to get the element with jQuery.
Now, you will need some Javascript, i will use jQuery code:
$('.radio').click(function (){
rad = $(this);
radRel = rad.attr('rel');
$('form#kl').attr('action', radRel);
});

There are multiple ways of doing it, depending on what you want exactly.
Check this one out, it might help you get there; Radio Button to open pages

You can use form.submit() as onclick-handler (not onchange) and change the action, too.
<input type="radio" name"rubrik" value="private" onclick="this.parentNode.action='yourOtherFile.php'; this.parentNode.submit()"></input>

Related

JQuery Validation With Multiple Sets Of Radio Buttons

I have a form that did submit 1 out of 8 radio buttons to a php $_POST super global array. Therefore, I needed some validation. I was kindly helped by being provided this code which works great:
$("#form").submit(function (event) {
if(!$(":radio:checked").length) {
alert("You must select at least one emotional state!");
event.preventDefault();
}
});
However, I have now been asked to have 2 sets of 8 radio buttons in which the user selects 2 answers instead of the initial 1 answer. I need the code to be able to determine that at least one radio button from each set of 8 buttons has been selected before the form is submitted. At the moment the code checks to see if any radio buttons have been selected, then as soon as just 1 buttons is selected, the function is satisfied and transitions to the next page, which is not what I want.
EDIT
Buttons code:
<p><input type="radio" value="happy" name="perceived_emotion">Happy
<input type="radio" value="excited" name="perceived_emotion">Excited
<input type="radio" value="angry" name="perceived_emotion">Angry
<input type="radio" value="frustrated" name="perceived_emotion">Frustrated
<input type="radio" value="miserable" name="perceived_emotion">Miserable
<input type="radio" value="sad" name="perceived_emotion">Sad
<input type="radio" value="tired" name="perceived_emotion">Tired
<input type="radio" value="relaxed" name="perceived_emotion">Relaxed</p>
<p><input type="radio" value="happy" name="induced_emotion">Happy
<input type="radio" value="excited" name="induced_emotion">Excited
<input type="radio" value="angry" name="induced_emotion">Angry
<input type="radio" value="frustrated" name="induced_emotion">Frustrated
<input type="radio" value="miserable" name="induced_emotion">Miserable
<input type="radio" value="sad" name="induced_emotion">Sad
<input type="radio" value="tired" name="induced_emotion">Tired
<input type="radio" value="relaxed" name="induced_emotion">Relaxed</p>
Here is the form code:
<form id="form" action="audio_handler.php?id=1" method="POST">
<div id="perceived_emotions">
<?php include("includes/induced_emotion_buttons.php"); ?>
</div>
<br />
<div id="induced_emotions">
<?php include("includes/perceived_emotion_buttons.php"); ?>
</div>
<p class="right"><input type="submit" name="submit" value="Submit"></p>
</form>
What you want to do is use the [name="value"] selector in JQuery in conjunction with the :checked selector. So your new code would be:
if(!$('input[name="perceived_emotion"]:checked').length) {
alert("You must select at least one perceived emotional state!");
event.preventDefault();
return false;
}
if(!$('input[name="induced_emotion"]:checked').length) {
alert("You must select at least one induced emotional state!");
event.preventDefault();
return false;
}
All of that wrapped into the form event.
EDIT: Since you only want to display one dialog at once, just add return false; in each if statement.

PHP multiple forms with single action

I've multiple forms with a single action, a single php page that gets called by all the forms.
How can I differentiate which form was sent to the php page?
Using a different unique input type="hidden" for each form.
HTML:
<input type="hidden" name="form_id" value="1">
<input type="hidden" name="form_id" value="2">
PHP:
$myform = $_POST["form_id"];
You can also use the submit button but note that the "value" parameter is what gets displayed to the user so you won't be able to modify it (assuming you want the same text to be displayed on every button).
<input type="submit" name="action" value="the user saw this">
PHP:
$_POST["action"] // -> "the user saw this";
Add a hidden field (action or the like) to each field, then check for it.
<form id="num1">
<input type="hidden" name="action" value="first_action" />
</form>
...and the check:
<?php
if(!empty($_REQUEST['action']) {
switch($_REQUEST['action']) {
case 'first_action':
// first action code
break;
}
}
?>
Give each submitbutton an other name or put a with different values in each form.
You can detect this from the submit button itself too, if submit has different values like:
update name, update profile, delete users...
On the submit button for each form, use different names. Something like:
<input type="submit" name="submit_1" value="Submit" />
<input type="submit" name="submit_2" value="Submit" />
<input type="submit" name="submit_3" value="Submit" />
...
Then on your PHP, you'll have:
$_POST["submit_1"]
$_POST["submit_2"]
$_POST["submit_3"]

Is there a way read all checkboxes in forms and have one Submit button in a separate form?

This question will show what a newbie I am. The situation is this. It's a photo contest.
People upload a photo and to the right on the same line is a checkbox.
Voters check the box if they like the photo. They can select up to, say, 5 photos.
Keeping it simple, my problem isn't with MySQL, but with the form. Each row has a checkbox, which is a form. The SUBMIT button is the problem. The only way I can figure out to have submit work is by putting a submit button with each checkbox. Of course, that's ridiculous. What I want is to read all the checked boxes and have ONE submit button when the voter is finished. Spent hours on this and can't see how to have the SUBMIT by itself that the voter can click and have all the checked values inserted into the database a one time.
Any notions? I know this sound very primitive, but just getting into this.
Thanks ahead of time for any help
You can enclose all the checkboxes in a single html form tag and have the submit button inside it. It'll automatically submit data in all checkboxes
Or
You can use javascript/jQuery to run through all the checkboxes. And submit the form.
Well, there are all sorts of javascript ways to go out and grab the data you want and submit it, but let's keep it simple.
[voteform.html]
<form action="process_script.php">
<img src="image1"><input type="checkbox" name="vote_for_image[]" value="1">
<img src="image2"><input type="checkbox" name="vote_for_image[]" value="2">
<input type="submit">
</form>
... then ... (please note the [RETURN ERROR...] and [DATABASE...] blocks are just place holders for you to fill in code)
[process_script.php]
<?
if (count($_GET['vote_for_image'] > 5) {
[RETURN ERROR 'Please select no more than 5 images to vote for']
}
else {
foreach ($_GET['vote_for_image'] as $index => $image_id) {
[DATABASE INSERT OR UPDATE FOR $image_id]
}
}
?>
Set the checkbox[] as name attribute for each checkbox and on submission you can address the valiues as arrays
<body>
<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">
<input type="submit" name="Submit" value="Submit">
</form>
<?php
if(isset($_POST['Submit']))
{
echo $_POST['checkbox'];
}
?>
</body>
the above will give you
Array (
[0] => a
[1]=>b
[2] => c
[3]=>d
)
Try this code
//HTML
<form method='post'>
<input type='checkbox' name='photo[]' value='1' />
<img src='test1.jpg' />
<input type='checkbox' name='photo[]' value='2'/>
<img src='test2.jpg' />
<input type='submit' value="Submit" name="submit" />
</form>
//PHP
if(isset($_POST['submit']))
{
if(isset($_POST['photo']))
{
if(count($_POST['photo']) > 5)
{
// Display error msg
}
else
{
// Contains all ids voted on
$img_ids=explode(',',$_POST['photo']);
// insert or update DB for the image ids
}
}
}

check box value in check form, php

I am modifying a php login form, adding javascript check form function to it. I wish when users tick the checkbox, the form is true, and when the checkbox is empty, the form becomes false. the codes are like these:-
//the javascript
function check_sli(form,mark,edit){
if(mark==1 || mark=="all"){
if(form.terms.value==""){
sli_check_terms.innerHTML="Please read the terms and conditions first!";
sli_check_terms.style.height="auto";
return false;
}else{
sli_check_terms.innerHTML="";
sli_check_terms.style.display = "none";
}
}
}
//the form
<form name="form_sli" id = "form_sli" ACTION="<?php echo $loginFormAction1; ?>" METHOD="POST" onSubmit="return check_sli(form_sli,'all')">
<input type="text" name="login"/><br>
<input type="password" name="password"/><br>
<input name="terms" type="checkbox" id="terms" checked="checked" onBlur="check_sli(form_sli,1)">I have read the terms and conditions<br><div id="sli_check_terms" class="right"></div>
<input type="submit" name="button" id="button" value="Login" />
</form>
The above codes works normal for textfields, such as when the textfield is empty, the innerHTML pops up. However, when using checkbox, I don't know the checked and unchecked value, is it 1 vs 0, or !=="" vs ==""??? and shall I use onBlur or onSubmit???
How shall it modify the scripts so that it works for checkbox as well? thanksalot!
checkboxes use .checked in the dom, which is a bool.
if (form.terms.checked) {
... it's checked ...
}
Instead of if(form.terms.value==""), do this if(form.terms.checked==false)

The Checkbox opens link onClick when form is submitted, but how do I post the form at the same time?

I am trying to build a downloads section where the visitor completes a form and then selects check boxes to open download dialogs.
I would like the form to submit and the dialogs to open, when the button is clicked.
I am not very good at writing scripts, although I can read them. Currently I can only get one or the other to work!
The form is a standard PHP form....
This is the javascript I am using
var suffix=1;
function downloadAll(oFrm){
var oChk = oFrm.elements["file"+(suffix++)];
if (oChk){
if (oChk.checked){
location.href = oChk.value;
setTimeout(function(){downloadAll(oFrm)}, 2000);
}
else{
downloadAll(oFrm);
}
}
}
This is an abbreviated part of my HTML
<input type="checkbox" name="file1" id="file1" value="/download.php?f=blank.gif" /><label for="file1">blank.gif</label>
<input type="checkbox" name="file2" id="file2" value="/download.php?f=atlas_blue_hover2.png" /><label for="file2">atlas_blue_hover2.png</label>
<input type="checkbox" name="file3" id="file3" value="/download.php?f=Tabs.css" /><label for="file3">Tabs.css</label>
<input type="button" value="Download All" onClick="suffix=1;downloadAll(this.form);return false;" />
I think that this might not be working because the onclick cancels out post in a PHP form
method="post" action="http://url"
Can I do similar to above using onSubmit? Or using the checkbox values in PHP?
Any help would be appreciated.
Don't return false from your onclick handler. That cancels the default action

Categories