Jump to content

Conditional form action...how?


dwest

Recommended Posts

Hi to all,
If I have a form such as:
[code]<form id="form_items" action="" method="post" enctype="multipart/form-data">
<input type="submit" name="btn_add_items" value="Add Items" />
<input type="submit" name="btn_del_items" value="Delete Selected Items" />
<input type="submit" name="btn_add_custom" value="Add Custom Items" />
</form>[/code]

I want the following conditions to be possible:
If "btn_add_items" is selected then post to add_items.php
If "btn_del_items" is selected then post to del_items.php
If "btn_add_custom" is selected then post to add_custom_items.php

How do I code that into the form action?

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/34749-conditional-form-actionhow/
Share on other sites

you can write that easily in javascript, but you can achieve the same purpose with php:

post to 1 single php file, say process.php

process.php:
<?php
if ($_POST['submit'] == "Add Items")
{
  // do this
}
else if ($_POST['submit'] == "Delete Selected Items")
{
  // do that,
}
.. and so on.
you can use something like this for java script:

function setFormAction(postTo)
{
  var thisForm = document.getElementById("form_items");
  thisForm.action = postTo;
  return true;
}


then on each of the button:
<input type="submit" name="btn_add_items" value="Add Items" onclick="setFormAction('add_items.php');"/>

and so on.
There are many ways to achieve your purpose.

java script, like my post above,

PHP, like my earlier post.

or you can submit to a single PHP file,
then insdie this PHP, call function buttonchoice,  this function have to include appropriate PHP file.
Thanks hvle,
I understand your PHP code.  I don't want to use javascript.  I can see that I inadvertently posted in the javascript forum...duh!  That caused confusion I'm sure.  My apologies.

I suppose what I need to know is can I have a php function on this form page that determines what submit button was clicked , and then have the form submit to itself and execute the function?  If so, what goes between the quotes in action="" ?

Thanks for your patience...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.