hello guys... I have an html page inwhich I collect some data using text boxes. Now in this page, I have button and I set the action attribute of <form> a php script like this.

<form action="test.php" method="post">
 -----

Now this does the job. But the problem is that I dont want to be redirected and want to stay on the same page but at the same time want to execute the test.php script. How can I do that? thnx

I think if you google after this:

<form action="PHP SERVER SELF"> </form

Which is it not called, but similar to that, then you wont have to go to another page, and probably need to include your form processing code in the same page too...

Hope it can help you :-)

<FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

And then write your processing code in the same page, og get it via an include file.

Member Avatar for diafol

Do you mean submit without redirect/reload? If so, use ajax.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

and

<form method="post">

Should do the same thing - forms are submitted to the same page by default.

It's not a good idea to send to the same page as you may passively re-send the form by reloading the page. A workaround is to send to a different page to handle the data (validation/DB update etc) and then redirect (e.g. to the form page again or elsewhere).

For Ajax, I suggest using jQuery. This will allow you to validate (client-side), pass the data to a php script and then return any data back to the jQuery script to update the page as required. All without page reload.

You can use vanilla-flavoured js for Ajax, but dealing with the The XMLHttpRequest Object is a pain and very messy.

When using:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

It is best to escape the PHP_SELF variable.

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

Older versions of PHP will include the URL parameters with PHP_SELF which allows an XSS injection if it isn't escaped. It is good rule to always escape any data written to HTML, unless you want it to be HTML specifically.

Hi

all of the above solution are right. you can choose any one of them.

Or you can use iframe . in iframe you can open another page in the same page

Gajendra

I think the OP had a different question. In fact this was the question I had too. We don't want the HTML form and the PHP commands in the same file. We want two separate files - the HTML code and the action.php. But when submit is pressed on the HTML form, the results don't open up a new window, they're displayed right below the submit button.

For specific example, I'm using Joomla and would like to not display the results in a new window. Rather, I'd like them displayed right below (ideally beside) the input form.

www.betterdivepro.com and on left hand nav, select MET Calculator. Fill out some numbers and press submit and you'll see what I mean.

Is this possible? Thanks kindly for any guidance.

jQuery is your friend on this :> Submitform without reloading via callback

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.