2

My problem:

I have index.html:

<form action="toload.php" method="post">
Input: <input type="text" name="something" value="" /><br />
<input type="submit" value="Submit!" />
</form>

toload.php is something like:

<?php

echo "Your input was: " . $_POST["something"];

?>

The question is quite simple.

When I press the Submit! button, I would like to dynamically load the content toload.php in index.html without the need of a refresh.

Thanks in advance! please comment for any needed clarification.


EDIT, a more verbose explanation:

I'm not sure I'm being clear (or maybe I'm not understanding the answers do to my lack of technical skills) so I'll give it another go. (re-write)

I have an HTML for with a submit button that sends a variable through POST method.

This variable is used by a PHP file and after a certain process, it inserts and update a MySQL database and echoes out some other stuff.

This is working JUST FINE.

But now I want to improve it by avoiding the page "reload" (going to the .php).

I want the HTHL that comes as an output of my HTML file to be dynamically shown in my HTML page.

Is it more clear now?

2
  • 1
    Check out api.jquery.com/load Commented Jan 29, 2011 at 23:00
  • 3
    $("#divname").load(url, { something: $("something").val(); }) should do it Commented Jan 29, 2011 at 23:01

3 Answers 3

3

something like this with jQuery!:

<form action="toload.php" method="post">
Input: <input type="text" id="something" name="something" value="" /><br />
<input type="submit" value="Submit!" onclick="submitme()" />
<div id="something2"></div>
</form>

and function to submit:

function submitme(){
var tosend=document.getElementById("something").value;
$.ajax({
        type: 'POST',
        url: 'toload.php',
        data: 'something='+tosend,
        success: function(msg){
            if(msg){
                document.getElementById("something2").innerHTML=msg;
            }
            else{
                return;
            }
        }
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

If you attach it to onclick then it won't fire when the user hits enter to submit.
well then attach it to that 2 :)
0

You must use AJAX (XMLHttpRequest) for that - http://www.w3schools.com/XML/xml_http.asp.

(You've said simply - as long loading 100KB of jQuery is not simply IMHO, I suggested pure JavaScript solution)

6 Comments

BTW, you may want to mention that jQuery is not 100kB.
@Trufa, XMLHTTPRequest is actually not that hard. It allows you to programmatically send a request in the same way that a browser does when you load a page. And you can capture the return value (the output of your PHP) and use it as you need. But this object is wrapped in JQuery which makes it more easy and better cross-browser compatible.
@singles I Trying this out but I don't know how to make the for POST the values to the .php so that the data is available when you load the page. This is as far as I've got.
@singles what I meant is that right know the content of the generar.php is being loaded, this is good. But the variables are not being sent, is that more clear?
@Trufa you must set valid content type and send data. Look at this: pastie.org/1511263 - I've added two lines and it's working now - data is sent by post and available on PHP side within $_POST.
|
0

In your case you can use $.post()DOCS. If you gave your form an id="myForm" then you could load the return data into the form's parent in the callback as follows:

$('#myForm').submit(function(e){
  e.preventDefault();
  $.post("toload.php", $("#myForm").serialize(), function(data) {
    $(this).parent().html(data);
  });
});

4 Comments

I think this is EXACTLY what I was looking for, I'll give it a try and come back in a sec! (Thanks) :)
I can't seem to make it work, I've tried this and this. But they all redirect the .php page as usual. AM I doing something wrong?
Sounds like you need to add e.preventDefault() to the function, I'll update my code.
Now I have this: pastie.org/1510427 but there is no reaction after the button click, thanks!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.