Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Send form data depending on radio button selection

    I have two radio buttons. I will call one radio button (Review) and the other (Submit) to make things easier to explain. If i select Review and click the submit button I want the form data to go to ./review.php if i click the Submit radio button then click the submit button i want the form data to go to ./submit.php

    Code:
    (Review)<input type = "radio" name = "ra1" value = "./review.php"><br>
    Code:
    (Submit)<input type = "radio" name = "ra1" value = "./submit.php"><br>
    The Submit Button
    Code:
    <input type = "button" name = "butto" value = "Submit" onclick = "subWhere()">
    So the issue I have is when i click on Review radio button it goes to ./review.php and when i click on the submit radio button it goes to ./submit.php, however, none of the form data gets posted on the php page. (the subWhere function works fine just not carrying over the data)


    I know I need to use form action and method =post but I don't know where that would go in the above code. Any ideas?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,946
    Thanks
    0
    Thanked 236 Times in 233 Posts
    PHP Code:
    <input type="submit" value="Submit" onclick="this.form.action = this.form.ra1[0].checked ? this.form.ra1[0].value : this.form.ra1[1].value;" /> 
    You have to set the default selected option:
    PHP Code:
    (Review)<input type "radio" name "ra1" value "./review.php" checked="checked" />
    (
    Submit)<input type "radio" name "ra1" value "./submit.php" /> 

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    You can make both possibilities go to the same processing page, but use switch/case to choose the processing option. This works very well for many options and is easy and clean to code.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #4
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by glenngv View Post
    PHP Code:
    <input type="submit" value="Submit" onclick="this.form.action = this.form.ra1[0].checked ? this.form.ra1[0].value : this.form.ra1[1].value;" /> 
    You have to set the default selected option:
    PHP Code:
    (Review)<input type "radio" name "ra1" value "./review.php" checked="checked" />
    (
    Submit)<input type "radio" name "ra1" value "./submit.php" /> 
    Thanks for your reply. I am still having the issue of grabbing the data from the form in the .html to display on the .php pages. It works fine if I use.
    Code:
      <FORM METHOD=Post " ACTION="./submit.php " name="myForm">
    But when I implement the radio button option to bring the data to either submit.php or review.php (depending on selection) it will go to that page but none of the content will carry over and display. Any ideas?

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,946
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Can you post the updated code/html?

  • #6
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Code:
    <select name="folks" onchange="showPic(this);">
     
     
    <option value="Pick Your Name">Pick Your Name</option>
    <option value="Mary Jane"> Mary Jane</option>
    <option value="Mike Smith">Mike Smith</option>
    
    
    <INPUT TYPE=RADIO NAME="gam" VALUE="male">male<BR>
    
    <INPUT TYPE=RADIO NAME="gam" VALUE="female">female<BR>

    Java script function
    Code:
    <script type = "text/javascript">
    function subWhere() {
    var selected = 0;
    for (var i = 0; i < document.myform.rad1.length; i++) {
    if (document.myform.rad1[i].checked) {
    selected = 1;
    window.location = document.myform.rad1[i].value;
    }
    }
    if (selected == 0) {
    alert ("Please select to review or submit");
    }
    }
    Code:
    Html to determine where to submit
    (Review)<input type = "radio" method="post" name = "rad1" form action = ". /review.php"><br>
    (Submit)<input type = "radio" name = "rad1" value = "./submit.php"><br>
    Code:
    Submit button
    <input type = "button" name = "butto" value = "Submit" onclick = "subWhere()">

  • #7
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,471
    Thanks
    6
    Thanked 980 Times in 953 Posts
    This should really not be done with JavaScript in the first place. What do we have PHP for anyway?

    Submit data to form processing script (PHP) which reads the value of the selected radio button (can be anything) and returns the appropriate data. No need to have two PHP scripts and a script to decide to which script the form will be submitted. That’s a stupid detour.
    Last edited by VIPStephan; 10-29-2013 at 10:41 PM.

  • #8
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    Quote Originally Posted by connollyc4 View Post
    I have two radio buttons. I will call one radio button (Review) and the other (Submit) to make things easier to explain. If i select Review and click the submit button I want the form data to go to ./review.php if i click the Submit radio button then click the submit button i want the form data to go to ./submit.php
    I started to critique the code you've shown, but since it's so badly written and you've not bothered to provide your entire HTML and JavaScript, I'll just provide a demo instead. I've created an example form that demonstrates how to do this using the HTML5 formaction and formtarget attributes with a fallback script for browsers that don't support them (like Internet Explorer 9).

    Live Demo: https://patrick.dark.name/web.dev/de....submission.2/

    Demo Core Code (default.xhtml):
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="application/xml" href="../style.sheets/boilerplate.xslt"?>
    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:p="https://patrick.dark.name/">
    	<head>
    		<title>Demo for CodingForums.com Thread 306083: Send form data depending on radio button selection</title>
    		<p:title>Demo for CodingForums.com Thread 306083: <cite>Send form data depending on radio button selection</cite></p:title>
    		<style>
    			.button\.and\.frame { float: left; margin-right: 1rem; }
    			#buttons\.and\.frames { overflow: auto; }
    		</style>
    	</head>
    	<body>
    		<main id="content">
    			<section id="description">
    				<h1>Demo Description</h1>
    				<p>This demo demonstrates how to submit a single form to two different <abbr title="uniform resource locators">URLs</abbr>.</p>
    			</section>
    			<section id="demo">
    				<h1>Demo</h1>
    				<form id="submit.color" method="post">
    					<label for="color" class="control-after">Color</label>
    					<select id="color" name="color">
    						<option>Blue</option>
    						<option>Green</option>
    						<option>Indigo</option>
    						<option>Orange</option>
    						<option>Purple</option>
    						<option>Red</option>
    						<option>Yellow</option>
    					</select>
    					<div id="buttons.and.frames">
    						<div class="button.and.frame">
    							<button id="button.for.page.1" formaction="page.1.xhtml.php" formtarget="page.1">Submit to Page 1</button>
    							<iframe id="page.1" name="page.1" src="page.1.xhtml.php"/>
    						</div>
    						<div class="button.and.frame">
    							<button id="button.for.page.2" formaction="page.2.xhtml.php" formtarget="page.2">Submit to Page 2</button>
    							<iframe id="page.2" name="page.2" src="page.2.xhtml.php"/>
    						</div>
    					</div>
    				</form>
    			</section>
    		</main>
    		<script>
    			// <![CDATA[
    			// This script was validated at http://jshint.com/ using the following settings:
    			/* jshint browser: true, curly: true, eqeqeq: true, devel: false, forin: true, immed: true, latedef: true, newcap: false, noarg: true, noempty: true, nonew: true, plusplus: true, quotmark: double, undef: true, unused: strict, strict: true, trailing: true */
    			(function () {
    				"use strict";
    				var XHTML = "http://www.w3.org/1999/xhtml";
    				function submitForm(event) {
    					/* jshint validthis: true */
    					event.preventDefault();
    					var form = document.getElementById("submit.color");
    					form.setAttribute("action", this.getAttribute("formaction"));
    					form.setAttribute("target", this.getAttribute("formtarget"));
    					form.submit();
    				}
    				if (typeof document.getElementsByTagNameNS(XHTML, "button").item(0).formAction !== "string") {
    					var buttonForPage1 = document.getElementById("button.for.page.1");
    					var buttonForPage2 = document.getElementById("button.for.page.2");
    					buttonForPage1.addEventListener("click", submitForm);
    					buttonForPage2.addEventListener("click", submitForm);
    				}
    			})();
    			// ]]>
    		</script>
    	</body>
    </html>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,946
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by connollyc4 View Post
    Code:
    window.location = document.myform.rad1[i].value;
    You are wondering why your the php code cannot grab the submitted data when you are just redirecting to the php page and not actually submitting the form. Had you followed what I suggested, it would have worked. But I agree with VIPStephan that the best solution is to submit to a single php page and from there determine the appropriate action.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •