html button to display php script results [duplicate] - php

This question already has answers here:
PHP form - on submit stay on same page
(11 answers)
Closed 4 years ago.
I have this script in a separate file as my index.php.
I want to have a button that if pressed shows the results from my PHP script in the index.php, but when I use
<form action="../inc/script.php" method="post">
<input type="submit" value="Scan">
</form>
it just goes to the script.php page. how can I change it so it stays on index.php and print_r the array from the script on the index.php page?

The form action tells the browser to post the form results to that page. If you want to stay on the current page you'll need to remove the action. Then, you'll have to check the post and include the results within the current page.
<form method="post">
<input type="submit" name="Scan" value="Scan">
</form>
<?php
if (isset($_POST['Scan'])) {
include '../inc/script.php';
}
?>

One way to solve this problem is through using JavaScript, and specifically jQuery in my example.
What you would do is set up a POST request to the PHP document, that would return the print_r result. You also won't need a form in this instance, more than a button.
HTML:
<button type="button" id="scan"></button>
AND
<div id="returned-data"></div>
so you have a place to put the data when you get it back.
JS/jQuery:
$(document).ready(function(){
$('#scan').click(function(){
$.post('../inc/script.php').done(function(data){
$('#returned-data').html(data);
});
});
});
PHP (../inc/script.php):
<?php
//do something with your data, and finish with:
print_r($data);
?>

Related

Buttonprogramming in PHP [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 3 years ago.
So I've been trying to program a button that redirects you to another web page for quite some time now, and this is the Best solution I've come up with:
<form action="#" method="post">
<input type="submit" name="submit" value="LLL" />
</form>
<?php
$redirect_after_login='index.php';
if(isset($_POST['submit'])){
echo 'test'; //this was just a test
header('Location: ' . $redirect_after_login);
exit;
}
?>
So basically, what happens is that when I press the button I do not get redirected to the other page, but the echo command does emit test. Any ideas on how I could fix this? (Sorry for any gramar mistakes, english is not my first language, and if this is very obvious, I'm new to php :D)
You are most likely not actually executing the form through PHP. You're being redirected to the anchor # (action="#"), which does not result in a reload/re-request of the page, but only jumping to the top of the page. The action is mandatory to receive and process the forms data.
So try to change your forms action attribute to the filename of the PHP-File or just leave it blank to send the form data to "the same page".
<form action="" method="post">
<input type="submit" name="submit" value="LLL" />
</form>
Also you should do any processing first, and output last. Move the form to the bottom of the file, because header() can't send its headers when something has been output before (your form in this case).

html button to load page with sql script but then return to current page [duplicate]

This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 3 years ago.
I was trying to make an html button load some sql script, and the way I figured best to do it was with an html form using post method and loading another php page which contains the script. The problem is it then just shows a blank page once it completes but I would rather reload the previous page.
Originally I wanted to just load the script on the same page and refresh it but found this to be easier.
Here is what I have so far:
<form action="move_to_processing.php" method="post" onsubmit="return confirm('Are you sure you want to move them??');">
<input type='submit' name='hehe' value="Move to processing" />
</form>
and then the page move_to_processing.php loads which is just a page with the sql. This is working right now.
I tried doing the following to just reload the current page (orders.php) but it didn't seem to work:
<form action="orders.php" method="post" onsubmit="return confirm('Are you sure you want to move them??');">
<input type='submit' name='hehe' value="Move to processing" />
</form>
<?php
if(isset($_POST['hehe'])){
tep_db_query("update orders o set o.orders_status= 13 where o.orders_status = 12");
}
}
After you execute your function, you add Location header to your response, and then immediately end script execution. When the browser receives the Location header it will redirect the user to that page.
<?php
// orders.php
if(isset($_POST['hehe'])){
tep_db_query("update orders o set o.orders_status= 13 where o.orders_status = 12");
header('Location: /previous-url');
exit();
}
?>

HTML and PHP in one file

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

Pass javascript array to server using jquery .post on submit and then print array in PHP?

HTML:
<form id="continue" action="summary.php" method="post">
<input type="submit" value="Continue" >
</form>
JS/JQuery
$('#continue').submit(function(){
var data = ["jon", "steve"];
var nameArray = JSON.stringify(data);
$.post("summary.php", { nameArray: nameArray });
});
PHP
$name = json_decode($_POST["nameArray"]);
print_r($name);
When I click the "Continue" button, I want to be redirected to the summary.php page and list the names. Currently, I get an "Undefined index: nameArray error when the summary.php page is loaded.
I checked firebug and there is no POST. So it looks like nameArray isn't even being posted either. What's the solution?
This is not the correct way of handling data.
You are posting a page (leaving it) and doing an ajax post within? If you want to stay on the page, you use an ajax post. If you are posting data normally, you use a form post.
If you want to post the name jon or steve as an array you can use the following html
<input type="hidden" name="nameArray[]" value="jon" />
<input type="hidden" name="nameArray[]" value="steve" />
If you want to use ajax, then just dont do it on the submit of a form. You can do it on a submit, but then return false at the end of the function so the page will not be submitted (you will stay on the current page)

Search MySQL with PHP and display results on the same page

This is definately a novice question, but if you could be of any help i would be very grateful.
Basically, i'm building a database management page, and it of course includes a search function.
So, the search form looks something like this
<form name="name" function="search.php" method="get">
But, whenever i use it, i will of course get redirected to search.php. What i want is a way to display the results on the same page i did the search from (let's say index.php), without having to build an entire identical page around search.php
Thankful for answers.
Use a hidden field in the form that indicates that the form has been submitted.
In your form page (e.g. index.php)
<form name="name" action="index.php" method="post">
{OTHER_FORM_FIELDS}
<input type="hidden" name="doSearch" value="1">
</form>
So in your php code (could be in the index.php page or in a php script included)
<?php
if($_POST['doSearch']==1) {
//query database
//get results
} ?>
in your index.php page
<?php if($_POST['doSearch']) { //a search request was made display my search results ?>
HTML_CODE
<?php } ?>
Let the page submit to itself:
<form name="name" function="index.php" method="get">
In the handler for the page, check whether or not you have parameters and display either the input box or the results as appropriate.
You could even take it one step futher. You could use AJAX to insert the results directly into the page content when the submit button is pressed, rather than causing a page refresh.

Categories