Jump to content

Different Actions for Different Form Inputs...


jwk811

Recommended Posts

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?
Link to comment
Share on other sites

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

[quote]Note: A form can only contain [b]ONE[/b] action.[/quote]

Furthermore, Sir AndyB is correct. What exactly do you want to happen?  ???
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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 [i]anything[/i] 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 [i]you want[/i] will get us where we want to be faster than if we guess  ;)

Link to comment
Share on other sites

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

the form will be something like this
[code]<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?[/code]
Link to comment
Share on other sites

[quote author=jwk811 link=topic=110189.msg444973#msg444973 date=1159753747]
this was exactly what i needed, did you know about this?
[/quote]

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.

Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

You could use Ajax for something like this.  

Here is the javascript.

[code=php:0]
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;
}

[/code]

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.

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

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

Hope this example helps..

Tom

     
Link to comment
Share on other sites

[quote]the form will be something like this[/quote]

[quote]this was exactly what i needed[/quote]

[quote author=jwk811 link=topic=110189.msg444997#msg444997 date=1159758291]
still isnt working....
[/quote]

Do we get to see your code, or should we continue to guess?
Link to comment
Share on other sites

Heres the form:
[code]<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>[/code]
And heres the process script
[code]<?php

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.