Jump to content


Photo

Different Actions for Different Form Inputs...


  • Please log in to reply
15 replies to this topic

#1 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 02 October 2006 - 12:46 AM

Yeah, and how would I do that. Say if someone has a list of things in a form and each of them will go to a different page if chosen. How can I do that? Something with the if...else... elseif statment? Some code would be great, thanks.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 October 2006 - 12:50 AM

While 'some code would be great', a good start would be for you to be rather more specific about what you actually want to happen - examples are great for explanations.
Legend has it that reading the manual never killed anyone.
My site

#3 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 02 October 2006 - 01:01 AM

someone will get a form...

there will be a few selections, each which go to another page...

like

<select>
<option>Choose this to go to page 1</option>
<option>Choose this to go to page 2</option>
</select>

Along with the submit.. how will i make this work?

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 October 2006 - 01:12 AM

Something here - http://javascriptkit...utindex16.shtml - will do what you want.
Legend has it that reading the manual never killed anyone.
My site

#5 marknt

marknt
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts
  • LocationPhilippines

Posted 02 October 2006 - 01:14 AM

Try something like this:
<form action="<?=$link?>" method="post">
Where the link can be the file itself (" $_SERVER['PHP_SELF'] ") or something like ./edit.php depending on the situation.

Note: A form can only contain ONE action.


Furthermore, Sir AndyB is correct. What exactly do you want to happen?  ???

#6 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 02 October 2006 - 01:26 AM

like could i to the if..elseif thing to do this.. say i send the form data to a script and in the script it says if they chose "this" then redirect to... with the "Location: url" .. would you know how i could find out which thing was chosen by the user in the next page? would i do the $_POST thing and... well how would i know if something was chosen.. if its "true"?

and what did you mean marknt with the link thing? how would that work?

#7 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 October 2006 - 01:37 AM

I assume that the link to all sorts of different javascript redirection scripts isn't what you want. So, you want 'something' different .... with ifs, elses, and if then elses ...

Does the script that is receiving the form information do anything with it other than decide to go to www.wombats_are_us.com if 'wombats' is selected from a dropdown and go to www.cnn.com if 'news' is selected.  As I said, a rational description of what you want will get us where we want to be faster than if we guess  ;)


Legend has it that reading the manual never killed anyone.
My site

#8 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 02 October 2006 - 01:49 AM

thanks for your help, i found what i needed from another thread

the form will be something like this
<form action="reffer.php" method="POST">
<input type="radio" name="page" value="1">Page 1<br>
<input type="radio" name="page" value="2">Page 2<br>
<input type="radio" name="page" value="3">Page 3<br>
</form>
code]
and the reffer.php script will be this
[code]
<?php
if(isset($_POST['page'])){
switch($_POST['page']){
case 1:
header("Location: page1.php");
break;
case 2:
header("Location: page2.php");
break;
case 3:
header("Location: page3.php");
break;
}
}
?>[/code]
this was exactly what i needed, did you know about this?


#9 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 October 2006 - 01:53 AM

this was exactly what i needed, did you know about this?


Yes.  The original link I gave you included javascript equivalents, and one click away from it would have given you an exact duplicate of your code with javascript.  I'm glad you solved ypur problem.


Legend has it that reading the manual never killed anyone.
My site

#10 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 02 October 2006 - 02:05 AM

another problem.
the script that i send the form to emails me the data and now redirects depending on which value was chosen.
right now its only sending the mail and not redirecting.. does that have anything to do with it having two things to do? can it only handle one? any solutions here?

#11 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 October 2006 - 02:10 AM

Are there any more requirements for this form processing script?

Typically, retrieve the data sent by the form; validate it/do whatever you want with it; construct the email message; send email message; branch to whatever location you want depending on the value of something from the form.
Legend has it that reading the manual never killed anyone.
My site

#12 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 02 October 2006 - 03:04 AM

still isnt working....

#13 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 02 October 2006 - 03:24 AM

You could use Ajax for something like this.  

Here is the javascript.

function createRequestObject()
{
    if (window.XMLHttpRequest) { // Mozilla, Safari, Opera...
        var xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType)
			xmlhttp.overrideMimeType('text/xml');
    }
	else if (window.ActiveXObject)
	{ // IE
        try {
            var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!xmlhttp) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
	return xmlhttp;
}

var http = createRequestObject();

function sendRequest() {
    var field = document.getElementsByTagName('nameOfField').value;
    http.open('POST', 'yourmailscript.php');
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencode');
    http.send('field=' + field);
    http.onreadystatechange = handleRequest();
}

function handleRequest() {
   if (http.readyState == 4) {
      //the response from the mail script.
      var response = http.responseText;
      if (response == 'sent') {
         redirector();
      } else {
        alert('There was an error in sending the mail');
      }
   }
}

//this will redirect the user as needed

function redirect() {
    var field = document.getElementsByTagName('nameofInputField').value;
    if (field == 'something') {
        window.location = 'http://www.something.com/something.php';
    }else if (field == 'something else') {
        window.location = 'http://www.somethingelse.com/whatever.php';
    } else {
        alert('invalid location');
    }
}

function blockSubmit() {
    sendRequest();
    return false;
}


Now in the form you could do something like this..

<form action="" onSubmit="return blockSubmit();">
<!-- your drop down menu -->
<input type="button" value="Submit" onClick="sendRequest();" />
</form>

Now in your php script that receives the information from the script above. You could do something like this.

$field = strip_tags($_POST['field']);
//now you send your email

if (mail($to, $subject, $message, $headers)) {
   echo "sent";
}else{
   echo "error";
}

Hope this example helps..

Tom

     

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#14 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 October 2006 - 10:40 AM

the form will be something like this


this was exactly what i needed


still isnt working....


Do we get to see your code, or should we continue to guess?
Legend has it that reading the manual never killed anyone.
My site

#15 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 02 October 2006 - 11:24 PM

Heres the form:
<table>
<tr>
<form action="process.php">
<td style="font-size: small"><b>Method</b></td>
<td><input type="radio" name="1" value="1">
<option>Live Chat</option>
<input type="radio" name="2" value="2">
<option>Checkout Lane</option>
</td>
</tr>
<tr>
<td>
<input type="submit"></td>
</tr>
</table>
</form>
And heres the process script
<?php

if(isset($_POST['page'])){
switch($_POST['page']){
case 1:
header("Location: http://www.boldchat.com");
break;
case 2:
header("Location: lane.php");
break;
}
}
?>
When the form is submitted it just goes to the process page and doesnt do anything else.

#16 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 October 2006 - 11:32 PM

If you omit the method from the form opener, it defaults to method="get".  You can either test the $_GET array in your second script, or change the form opener to include method="post".
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users