homepage Welcome to WebmasterWorld Guest from 54.211.157.103
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
dynamic form action?
What is best method to accomplish the PHP
Merganser




msg:4415701
 3:57 am on Feb 9, 2012 (gmt 0)

I have a form that I want submitted to different scripts depending on the selections made by the user in the form. In other words, I want the form action to be dynamic (i.e., "page1.php" below would change depending upon how the user fills out the form).

<form action="page1.php" method="post">

I figure I could have the form data submitted to one script that inspects the data and redirects to different pages using the header(); command. But I wonder if there is a better or more-highly-recommended way of accomplishing the same effect in php.

Any suggestions or guidance would be appreciated.

 

ZakAltF4




msg:4416327
 4:59 pm on Feb 10, 2012 (gmt 0)

If I am understanding you correctly, this shouldn't be handled via PHP ... It should be a JavaScript (More specific -- JQuery) issue. You could use something like:


//set your function parameters -- onclick .. onchange etc ..
$('#formId').attr('action', 'page1.php');

// more parameters
$('#formId').attr('action', 'page2.php');

// More paramenters
$('#formId').attr('action', 'page3.php');



That's how I would go about it ..

-- Zak

rocknbil




msg:4416331
 5:32 pm on Feb 10, 2012 (gmt 0)

You don't need it to submit to different scripts, or even make it javascript dependent, just create an intermittent script that makes the decisions for you, or, include different files based on the submit. Example,

<form action="page1.php" method="post">
<select name="show-me" id="show-me">
<option value="">Select</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
<input type="submit" value="submit">
</form>

Then in page1.php,

$possible_values = array(
'red' => 'red.php',
'green' => 'green.php',
'blue' => 'blue.php'
);

if (isset($_POST['show-me']) and array_key_exists($_POST['show-me'],$possible_values)) {
include($possible_values[$_POST['show-me']);
}
else {
echo "<p>Invalid input! Please use form below.</p>";
// re-output form here!
exit;
}

A couple things about this approach:

- It is (pretty close to?) impossible to hack because it adheres to the basic premise of security: accept only what you want, throw everything else away. Any values submitted except red, blue, or green for show-me are rejected.

- one might say why don't you do this?

$include_file = $_POST['show-me'] . '.php';

include($include_file);

This does open the file to injection of malicious data. I could post anything I want to your script and maybe include something I shouldn't see.

- Now you can use Javascript for what it's intended (in this case:) assisting the user in using the form properly.


...
<head>
<script type="text/javascript">
window.onload=function() { attachBehaviors(); };
//
function attachBehaviors() {
var obj=document.getElementById('my-form');
if (obj) {
obj.onsubmit=function() {
if (document.getElementById('show-me').selectedIndex==0) {
alert('Please select an option.)';
return false;
}
return true;
};
}
}
</script>
</head>
<body>

<form action="page1.php" id="my-form" method="post">
<select name="show-me" id="show-me">
<option value="">Select</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
<input type="submit" value="submit">
</form>
....

If you feel you must submit to different scripts, then use the same logic to print a header and carry all the submitted values. That seems like more work though with redundant data . . . same headers/template, duplication of a lot of programming . . .

Merganser




msg:4416616
 4:50 pm on Feb 11, 2012 (gmt 0)

Thanks for your comments.

I realize I could do this with javascript but do not want to rely on it, hence my posted interest in a php solution.

The include() would be problematic for my case because I want the URL to reflect the different pages that the user is routed to.

Basically, I am trying to control the URL displayed (in addition to the content displayed).

Any other thoughts?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved