Chen Xiao Xiong
Chen Xiao Xiong

Reputation: 25

HTML form method post does not work with multiple submit

HTML form method post does not work with multiple submit

I have the following

<form id="myForm" method="post" action="">

<select name="select_data">
<option value="1000">Account Demo 1</option>
<option value="1035">Account Demo 2</option>
</select>

<input type="submit" onclick="sendForm('<?php echo $domainURL;?>page_1')" form="myForm" class="btn btn-primary btn-sm" value="Page 1">

<input type="submit" onclick="sendForm('<?php echo $domainURL;?>page_2')" form="myForm" class="btn btn-primary btn-sm" value="Page 2">

<script>
    function sendForm(action)
    {
        document.getElementById('myForm').action = action;
        document.getElementById('myForm').submit();
    }
</script>

I did some testing, I change method to "get" and its work, but when I change method to "post" , it not able send anything.

I tried with

print_r($_GET);
print_r($_POST);

Get was able to retrieve the send value on a select option tag. Same code, but post doesn't send through

I tried to do post

if action="" , its work fine
but if action="http://www.myowndomain.com/subpage/page/thepage.php" it does not work for post. 

Upvotes: 0

Views: 786

Answers (2)

Barmar
Barmar

Reputation: 781068

Instead of Javascript, use the formaction attribute:

<input type="submit" formaction="<?php echo $domainURL;?>page_1" class="btn btn-primary btn-sm" value="Page 1">

<input type="submit" formaction="<?php echo $domainURL;?>page_2" class="btn btn-primary btn-sm" value="Page 2">

THere's also no need for form="myForm" when the submit button is inside the <form> element.

Upvotes: 1

user3510665
user3510665

Reputation: 218

Change <input type="submit"> to type="button" I hope it will work.

Upvotes: 0

Related Questions