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

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

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

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