I have two forms here, the first is a select dropdown select, which when posted generates another form, creating an amount input boxes based on the number selected. This second form then puts the data from the input boxes into 2 array. All this works perfectly but I am trying to add extra functionality to the submit button on the second form. I created a function 'showcontet()' to show and hide the content in the div with id 'table'. This works perfectly when used on a button outside of the form. When used on the submit button inside the form you can see the content for about 1 second before it then disappears again.
<head>
<script type="text/javascript">
showcontent = function(){
var content = document.getElementById('tabl');
content.style.display = 'inline';
}
</script>
<?php
$userkey = array();
$userval = array();
$usernum = $_POST['usernum'];
$total = 0;
?>
<form action='MYPIE.PHP' method='POST'>
HOW MANY SECTIONS?
<select name="usernum" value="<?php echo $usernum;?>">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
</select>
<input type="submit" name="submitnum" value="submit selection" />
</form>
<form action='MYPIE.PHP' method='POST'>
<?php
for ($i=1; $i<$usernum+1; $i++){
echo '<input type="hidden" value="' .$usernum.'" name="usernum" />';
echo "<br>insert key: <input name='key".$i."' value='".$userkey[$i]."'> insert value: <input name='val".$i."' >";
$userkey[$i] = $_POST["key".$i];
$userval[$i] = $_POST["val".$i];
}
?>
</br>
<input type="submit" value="submit" name="submit keys" onclick="showcontent()" />
</form>
</table>
<div id="tabl" style="display:none;">
content
</div>
Apologies for the bad indentation, I am pretty new to this & thanks for any help
-
Sorry if the explanation didn't make sense. I am not trying to stop the form submitting, I am trying to get it to submit the data and run the showcontent() at the same timeKza– Kza2012年07月06日 12:50:49 +00:00Commented Jul 6, 2012 at 12:50
-
If you submit the form it will send a server request, so the page MYPIE.PHP will load. If you want to run showcontent() to change some div content and also submit the form, you should use ajax.Angel– Angel2012年07月06日 13:00:09 +00:00Commented Jul 6, 2012 at 13:00
3 Answers 3
You need to return false from the event handler to prevent the browser from submitting the form and loading the page.
1 Comment
make your second button not a 'submit' button but a regular 'button' else it will fire the submit of the form...
or
set the 'showcontent()' function on the forms 'onsubmit' event and let that function return false:
onsubmit="return showcontent()"
function showcontent(){
var content = document.getElementById('tabl');
content.style.display = 'inline';
$.ajax({
type: "POST",
url: "MYPIE.PHP",
data: { usernum: $("select[name='usernum'] option:selected").val() }
}).done(function() {
alert('data posted');
});
return false;
}
6 Comments
You need add e.preventDefault(); to the event handler to prevent the browser from submitting the form and loading the page.
Don ́t use return false as this is other sideeffects you might not want. see http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/