最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - change forms action with javascript - Stack Overflow

programmeradmin3浏览0评论

I have a form that its action was initially to refresh the page. but I need a button that will submit the data and change the form's action. I have tried using JavaScript below:

<form id="form1" name="form1" action="" method="post">

Button

<input type="button" class="submit" value="Save" onchange="submitForm()" />

JavaScript

function submitForm(){
    document.getElementById('form1').action = <?echo base_url().'index.php/folder/controller/controller_function/';?>;
    document.getElementById('form1').submit();
}

When I click the button, nothing happens. I have also tried using if/else statement in the form's action, but that too doesn't work:

<form id="form1" name="form1" action="<?if(isset($_POST['saveButton'])){echo base_url().'index.php/folder/controller/controller_function/';} else{echo "";}?>" method="post">

I have a form that its action was initially to refresh the page. but I need a button that will submit the data and change the form's action. I have tried using JavaScript below:

<form id="form1" name="form1" action="" method="post">

Button

<input type="button" class="submit" value="Save" onchange="submitForm()" />

JavaScript

function submitForm(){
    document.getElementById('form1').action = <?echo base_url().'index.php/folder/controller/controller_function/';?>;
    document.getElementById('form1').submit();
}

When I click the button, nothing happens. I have also tried using if/else statement in the form's action, but that too doesn't work:

<form id="form1" name="form1" action="<?if(isset($_POST['saveButton'])){echo base_url().'index.php/folder/controller/controller_function/';} else{echo "";}?>" method="post">
Share Improve this question edited Aug 27, 2013 at 3:40 rink.attendant.6 46.4k64 gold badges110 silver badges157 bronze badges asked Aug 27, 2013 at 3:37 user2118738user2118738 51 silver badge4 bronze badges 4
  • 3 You should probably use onclick on the button. – ced-b Commented Aug 27, 2013 at 3:40
  • change button onchange attribute to onclick – Amila Commented Aug 27, 2013 at 3:43
  • you can also take a look at the "formaction" attribute in html5 – He Hui Commented Aug 27, 2013 at 4:05
  • omg,, of course, thanks alot everyone. i've changed my 'onchange' to 'onclick' and it worked perfectly. thanks. – user2118738 Commented Aug 27, 2013 at 6:09
Add a ment  | 

2 Answers 2

Reset to default 3

Firstly, the value in your JavaScript should be quoted, as it is a string:

function submitForm(){
    document.getElementById('form1').action = '<?echo base_url().'index.php/folder/controller/controller_function/';?>';
    document.getElementById('form1').submit();
}

Secondly, you're triggering the function on the change event of a button, which probably won't work because buttons do not change. Try binding to the submit event instead, on the form itself:

<form id="form1" name="form1" action="" method="post" onsubmit='submitForm();'>

But I'd do it in the JavaScript, using an event listener:

document.getElementById('form1').addEventListener('submit', function(e) {
    e.preventDefault();
    this.action = '<?echo base_url().'index.php/folder/controller/controller_function/';?>';
    this.submit();
}, false);

Put quotes around the action as shown below:

  document.getElementById('form1').action = "<?echo base_url().'index.php/folder/controller/controller_function/';?>";

You also need onClick instead of onChange

Works fine for me with these 2 changes.

发布评论

评论列表(0)

  1. 暂无评论