0

I have a 2 divs one for menu links and the other for content. What I want to be able to do is click a menu link and the page for that menu link to load in the content div.

I have already done this and it works. The problem is that the page inside the div contains a a form which does not come up with the success message and the database does not update.

All it does is either refresh to main content page or opens current window just as the content page or opens a new window with content page.

What solutions are there for this?

Logged_in.php

left div = menu

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

  $(".menuLink").click(function(){

$.ajax({
  type: "POST",
  url: $(this).attr('href'),
  data: "",
  cache: false,
  success: function(html){ $("#Middle").html(html); }
});

 });

});
</script>

</head>
<body>
    <div id="head">

<div class="lefttop" align="center">

  <div class="lefttopleft">
  <a class="menuLink" href="Test.php" onclick="return false;"><img src="Images/logo.jpg" width="94" height="21" /></div>

When click on that link the page loads into the content div so now that page is showing with a form on in content div what I am having the trouble with is filling form out clicking submit and it updatung the database it just refreshes to main content div or just page loads the the file click on or does so in new window.

Test.php in content div

session_start();
include_once("connect.php");
include_once("functions.php");

if($_POST['WallSubmit'] && $_POST['Wall']){

  $wall = makesafe($_POST['Wall']);

mysql_query("INSERT INTO `Wall` (`ID`, `Username`, `MessageBy`, `Date`, `Message`) VALUES ('', '', '$username', '" . time() . "', '$wall')");

}

?>

<body>
<form method="post">
  <table class="default" style="width: 80%" align="center">
<tr>
  <td class="subheader">Wall</td>
</tr>
<tr>
  <td class="content">Post a comment.<br /><textarea name='Wall' id='Wall' style='width: 99%; height: 110px;'></textarea><br /><br/><center><input type='submit' value='Post' name='WallSubmit' /></center><br /><hr><br />Latest Comments.</td>
</tr>
</td>
</table>
</form>
</body>
</html>

Is there an easy way round this I have looked everywhere?

kiamlaluno
  • 24,790
  • 16
  • 70
  • 85
  • The first paragraph of your post is very hard to read because it contains no punctuation whatsoever. The easier it is to read, the more likely your question will be answered. – Don Jun 25 '12 at 14:29
  • Also is there a way that one code submits all forms on different pages say to pages one code if im on once page submit form if on another page it submits form. or would it be code for each php seperate php file, Also note I am not a pro coder and a bit of a noob so as easy and and good understanding as possible, Thanks. – BoxyGILLETT Jun 25 '12 at 14:31
  • `
    ` where is the action attribute ?
    – The Alpha Jun 25 '12 at 14:35
  • Multiple forms can have the same value for their `action` attributes, yes. – Don Jun 25 '12 at 14:35
  • I have tried with action but it just reloads page to http://link/Test.php it does not I need to stay in logged_in.php so once form is submitted it only refreshes the content div and updates the database, You with me? – BoxyGILLETT Jun 25 '12 at 14:38

1 Answers1

1

First off, try to set an action on your form tag. The action defines the page, to which the form will be submitted.

<form method="post" action="Test.php">

With this the form should get submitted to Test.php. If it still doesn't do anything, try to print a variable dump (manual) with var_dump($POST); on top of the page. You will then see all set values of the post attributes. From there on it should be easy to find the error.

EDIT

After your comment I realized that you are trying to send the form with AJAX and just reload the content div. You have to do this in two steps. First, send the data of the form to Test.php, and then reload the content div. If stumbled upon this question: jQuery AJAX submit form. This may help you how to submit your form. If you want to use a return value, you can try something like this:

$.post("Test.php", $("#form").serialize(),
   function(data) {
     alert("Returned data: " + data);
   });

See jQuery.post()

Community
  • 1
  • 1
Slomo
  • 1,174
  • 7
  • 11
  • I have tried action but it just refreshes the whole page to Test.php i only want the content div to refresh and update database with form details. – BoxyGILLETT Jun 25 '12 at 14:46
  • If you only want to refresh the content div, you have to do it with ajax. As you are already using jquery, you should create a method with jquery. I will add a short help to my answer shortly. – Slomo Jun 25 '12 at 14:50
  • Ok thanks if there is any other easier way to get menu links when pressed to open in content div and use forms in content div then you can post that also if makes easier to other answer I dont mind changing a bit what ever is easier. – BoxyGILLETT Jun 25 '12 at 14:52
  • All I want to be able to do is click a menu link in one div page loads in content div and then for me to be able to fill out a form in the content div and it posts and updates the database. :) – BoxyGILLETT Jun 25 '12 at 14:53
  • You could achieve this in multiple ways. Probably the simplest for a beginner would be to use frames. Although it is nowadays a bit outdated, it still is very easy to implement. Another simple way would be, to create a menu.php, which you then include in your 'content-pages'. You could create a function printMenu(activePage) or something like that. There are numerous possibilities. Unfortunately it's been a while since I last programmed with PHP so I can't provide you a sample. – Slomo Jun 25 '12 at 15:03
  • Yeah all im getting is a white box popping up with all the page writing and code :S – BoxyGILLETT Jun 25 '12 at 15:17
  • You mean from the jquery example? It is just an example, obviously I don't know your intentions exactly. Maybe you should create a post in a forum, where you can discuss better. People can provide a sample page or something like that. – Slomo Jun 25 '12 at 15:32
  • My intentions is to submit a form and it updates the database just need it to post in a content div and reload to the Test.php within that div but thanks for youyr help – BoxyGILLETT Jun 25 '12 at 15:39