Hello and thank you for reading!
First things first, so here is my setup:
index.php - loads content into divs by using
jq('#p5-content').load('content/stuff.php');
- stuff.php - contains a form and a script in its
<head>
part.
This is the script:
var jq = $.noConflict();
jq(function () {
jq('form').on('submit', function (e) {
e.preventDefault();
var page = jq(this).attr("page");
jq.ajax({
type: jq(this).attr("method"),
url: jq(this).attr("action"),
data: jq(this).serialize(),
success: function (data) {
jq('#' + page + '-content').html(data);
}
});
});
});
This is the form:
<form action="content/stuff.php" method="post" page="p5">
<input type="hidden" name="p5-submit-1" />
<input type="image" style="margin-top: 20px;" src="images/send.png" />
</form>
I am running into the problem that sometimes when clicking the send button, the whole page is redirected to content/stuff.php
and sometimes only the wanted content inside the div (p5-content
) responds to the form. I want only the content of the p5-content
to be replaced.
Also when it worked and different content is then loaded into p5-content
which contains another (similar) form, the whole page changes to content/stuff.php
when clicking this second form.
Additionally I am wondering why the script doesn't work if it is in the <head>
part of my index.php
.
Summary:
- The script should work if it is in the index.php
- The script should always work and not just sometimes (often it only works after reloading the page)
- When submitting a second form inside the response area of my ajax the response should be inside the div again.