I have a form, on its action i have a php file, i want that when i click submit, it should first open the action php (means view in browser), and then start executing the code in it.
form tag is like this
<form action="myScript.php" method="post"></form>
right now what happen is, when i click submit, it stays on the same page and start executing the php file, when it is done then, then it shows the script file page in browser.
you can use jquery POST and do what ever you want:
jQuery.POST
Is the <input type="submit"> tags etc. inside the <form> tags. If not, then the input buttons are pointless, and just return false.
Your code should look like this:
<form action="myScript.php" method="post">
<input type="submit" />
</form>
Does this help?
Kai :-)
You have to execute the PHP in order to generate the page that is shown in the browser as a result of the POST. Even if your PHP is generating the page and then performing a long operation, output buffering and compression will defeat you, so you will need to turn them off. See How to flush output after each `echo` call? for other things you may have to do to get your output to appear immediately.
Related
I have a script that pulls an XML page and uses a form to update and save the values back. When I click the submit button it works, but then the page loads blank. I just want the page to refresh. There are about 100 different threads on this, and nothing I have tried has worked to resolve the issue. Out of curiosity, I just tried to run the window.location script and nothing else, and this piece actually doesn't work at all.
<?php
//if (isset($_POST['submit'])) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//$ctstatus->nodeValue = $_POST['ctstatusform'];
//htmlentities($xml->save('test.xml'));
echo '<script type="text/javascript">window.location="http://google.ca";</script>';
}
?>
The inner contents of the form don't really matter at this point, I just want it to refresh the page after I hit the submit button.
I previously used isset but from reading it seems like that's obsolete, and my form action="" used to be blank. Either way my XML save works, but nothing to refresh the page. I also tried header and that didn't work either.
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input class="save" name="submit" type="submit" value="Save" />
</form>
Out of curiosity I tried an onClick function with a timer and this does work but it's not ideal at all, especially because the page could technically refresh before the POST is finished writing the file. I'd rather know why the echo doesn't execute.
PHP redirect would most likely be preferable to JavaScript redirect.
Typical structure when posting back to same page:
<?php // cannot be any output before this (space, linefeed, etc)
if(isset($_POST['submit']) {
// do stuff with the submission
header('Location: http://google.ca');
exit;
}
// does your script need to do some other data retrieval or calculation? do it here.
?>
<html>
... snip ...
<form method="post">
... snip ...
<input class="save" name="submit" type="submit" value="Save" />
</form>
Following this simple structure for procedural scripts--
Deal with user input / redirect
Do logic (collect, manipulate data)
Show output (using php only to insert variables and looping)
will help you avoid a lot of heartache and technical debt.
Okay, I have gotten this sorted out. It turns out that the problem was embarrassingly simple, but maybe will assist someone in the future. Along with reordering my code, as Tim suggested. I specified HTML as the DOCTYPE, and that worked to resolve the issue. I no longer need to worry about refreshing the page after submit, because it refreshes as it should automatically. Thank you to everyone who commented.
My php code in one page
<?php
if(isset($_POST['gonder'])){....}
if(isset($_GET['ozelid'])){.....}
?>
<input type="submit" name="gonder">
<td> <a href=\"?ozelid=YERTUTUCU&fiyat=$haskur\" onclick=\"return confirm('Tahsil Edilsin?')\" >Tahsil</a>
First time after the page loads ,when I click submit, it is okay and it only runs if(isset($_POST['gonder'])){....}
Again first time after the page loads , when I click the link then it only runs
if(isset($_GET['ozelid'])){.....}
However, after the page loads, when click the link and then I click the submit button both if(isset($_POST['gonder'])){....} and if(isset($_GET['ozelid'])){.....} run, which is not desired.
If you only want one of these being called at a time, then I would suggest just using an ElseIf.
<?php
if(isset($_POST['gonder'])){....}
Elseif(isset($_GET['ozelid'])){.....}
?>
You shoud check your form tag:
If you are using <form action="#" or keep the action attribute empty, the form is sent to the current page (in your case it's the page having the GET parameters).
Please check if you have the possibilty to set the action attribute of your form tag to something like this:
<form action="index.php" method="post">
You have to replace index.php with the name of your php file.
After you processed the request you can send location header. Like so:
header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
die();
To your current url without get parameters. I guess this should be in your code in this block:
if(isset($_GET['ozelid'])){.....}
I have the following hidden input in a form:
<input type="hidden" name="token" value="<?php echo $token; ?>">
I am posting this input value to another PHP page for processing through a form, however, when I try to read the value of the input field using $_POST["token"], I find it empty. I looked all over the Internet for a solution, but the only one I found is to place everything into one page (the form together with the processing code); but I want the processing code to be in a separate page.
This is the markup:
<form id="registerform" name="registerform" method="post">
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
<input type="hidden" name="token" value="<?php echo $token; ?>">
</form>
The problem here is that I can read the values of the first name and last name. but the value of the hidden input is not accessible.
Your approach is correct. If it won't work like this, you probably made some typo or mistake and need to debug your code. Here is one way to do that.
Check if the value $token is correctly rendered into the <input>. Does it have a value in the PHP script or is already empty there? If it is not empty in the PHP script (you can echo it out, to see its value), you need to check the rendered HTML. Does it have any mistakes? It's best to just open the view-source tab to see what PHP actually renders. Somewhere in the rendered HTML, there needs to be a hidden input field with the value set to the same as in the PHP script. Check also the syntax.
If 1 did not yet resolve your problem, you can try to send the form and observe the request in the DevTools of your browser (it's probably best to use Firefox or Chrome for that). To see the request, you should open the DevTools (push F12) and switch to the Network tab. Make sure, you hit Preserve log to keep the logs on the page switch. Click on the row of the page where your data should have been sent to. You can see the parameters on the bottom of the details. Your hidden parameter needs to be there, if not, you are not correctly sending it to the PHP script. → Go back to step 1.
If the error still resists, enter the following line on top of the PHP page where your form is sent to (the page you have in the action attribute of the form): <?php print_r($_POST); ?>. This will print out all data which is received by the PHP script via POST (change it to $_GET, if you are expecting GET data). Submit your form. If the token key is present on the next page (e.g. [token] => "123" is part of the output), you can definitely access it via $_POST['token'].
I'm a PHP newbie trying to sort some basics out. I have a user-form that leads to a mysql select query, which works fine. Every tutorial I have found so far has the standard form tag, ie: action='script.php' method='post'. This obviously opens script.php in a new tab/window though.
If I don't want to display what's fetched from my db on a different webpage I have to put the html and php in one document together. I didn't think this is how you would really want to do it though.
My specific question is when you want to display stuff on the same page do you just put everything in together within one document and let users hit the submit button?
NO you dont put your php scripts on the same page as your html file/s
Try this link for your reference =)
OR you can put 2 different pages that act as 1 by using INCLUDE FUNCTION
script1.php
<form action="script2.php" method="post" name="myform">
...
<input type="submit" name='submit_button' value="Submit" />
<input
</form>
---------------
script2.php
include 'script1.php';
if(isset($_POST['submit_button']
{.......}
Yeah You can put html and php in single document.
With the help of action.But it not the proper way.
In action you should mention this for writing html and php in same page.
<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>
You can use the same page as Action in form and make condition based on your submit button whthere it is pressed or not.
If it is pressed you can make your Code there for connecting db and do operation like select, insert, update or delete.
e.g.
Your file: script.php
<?php
if(isset($_POST['btnsubmit'])) {
// Do your Operation here...
}
?>
<form action="script.php" method="post" name="myform">
...
<input type="submit" name="btnsubmit" value="Submit" />
<input
</form>
What you can do is simply refer the user back to the form, or another page on your server with the header tag. Inside your PHP script you'd add something similar after your query executes correctly
header( 'Location: ' . $_SERVER['HTTP_REFERER'] ); // Refer to the last page user was on...
Or another URI
header( 'Location: http://some.url/' );
If you really want to do this, here is a way:
<?php
if(isset($_POST)){
//do your php work here
}
?>
<html>
<form method='POST'>
//form elements here
<input type='submit'>
</form>
<!-- other html code -->
</html>
It depends on the length of your code, if the code is too much, then the better way is to include some script file to your parent file. using include() functions, and your perfect answer is yes. just put everything in together within one document
What I want
I want to upload a file without reloading the page, also I want to add the source link for the image to the textarea.
So when I push the upload_photo, the image uploads and a link is added to the textarea.
I want pure HTML, Javascript|AJAX and PHP.
What I have
<form action"index.php" method="post" enctype="multipart/form-data">
<textarea id="textarea" name="text"></textarea>
<input type="file" name="photo" />
<input type="submit" name="upload_photo"/>
<input type="submit" name="post"/>
</form>
Example sites:
http://www.friendfeed.com - the page don't reloads when you upload the files
What I don't want
Please avoid posting solutions with jQuery or any library, API.
That's easy.
Put an iframe, give it a name, for example "MyIframe".
Then in the form, add the TARGET attribute, with the value "MyIframe", and the action - the script that takes the upload (takeupload.php for example)
In the main page define a Javascript function that does something you need after the upload is done, which will be called, with parameters, from the page generated by takeupload.php.
in takeupload.php upload the image, then send as an output a normal blank HTML page that will execute a script which will call the method described above, with a set of parameters you need (image name, path, error, or plain HTML to insert somewhere, etc.).
use it like this:
<script type="text/javascript">
parent.YourJSMethod(parameters);
</script>
The page will be loaded in the iframe, and it will run a function defined outside the iframe. Upload is done, and the parent page receives data about the result.
This is fairly simple. No jQuery needed, no AJAX, no nothing, just a very simple Javascript code and a little HTML.
This can indeed be done with AJAX. I don't think that using AJAX is any more of a security risk than sending a vanilla HTML form; you will have to validate all user input on the server side all the same. Here's a simple example:
http://www.webtoolkit.info/ajax-file-upload.html