Jump to content

[SOLVED] Javascript and forms action help


AbydosGater

Recommended Posts

Hi guys,

Here, i am really stuck as dont know anything about javascript.

 

I am writing a form.. and one of the fields in the form is going to be for "Your Domain".

 

What i need help on, is figuring out.. when a user puts say.. domain.com in the box.. That the forms "action=" method is changed to the domain ie: <form action="domain.com/process_form">..

 

Javascript is the only way i can thing about doing this.. But i dont know Javascript.

 

Could someone please help me!

Andy

Link to comment
https://forums.phpfreaks.com/topic/60629-solved-javascript-and-forms-action-help/
Share on other sites

you could make a simple javascript function like so:

 

function submitForm(formid, actionURL)
{
  form_handle = document.getElementById(formid);
  form_handle.action = actionURL;
  form_handle.submit();
  return true;
}

 

and in the form's submit button, use:

 

<input type="submit" name="Your Domain" onclick="javascript:submitForm(thisformsid, 'domain.com/process_form');" />

 

the reason we still use a submit input type is so that those with javascript can still submit the form - it'll just head to the form's default action.

let's assume the ID of our input textbox is called "targetDomain" and that our form's ID is "formOfMysteryAndWonder":

 

function customSubmit()
{
  form_handle = document.getElementById("formOfMysterAndWonder");
  custom_action = document.getElementById("targetDomain").value;

  if (custom_action != '')
  {
    form_handle.action = custom_action;
    form_handle.submit();
    return true;
  }
  else
  {
    return false;
  }
}

 

submit button:

 

<input type="submit" name="do_this_thang" value="Go To Your Domain" onclick="return customSubmit();" />

 

give this a shot.  i have a habit of needing to troubleshoot my javascript functions, so it may not work out of the box, to warn you.

alright, well we might as well just drop the submit accessibility to do some bug checking:

 

function customSubmit()
{
  form_handle = document.getElementById("formOfMysterAndWonder");
  custom_action = document.getElementById("targetDomain").value;

  if (custom_action != '')
  {
    form_handle.action = custom_action;
    form_handle.submit();
  }
}

 

<input type="button" name="do_this_thang" value="Go To Your Domain" onclick="customSubmit();" />

 

keep in mind the javascript console in FF is extremely handy for javascripting.

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.