Buttonprogramming in PHP [duplicate] - php

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).

Related

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 button to display php script results [duplicate]

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);
?>

echo $_POST['code']; error in google chrome [duplicate]

This question already has answers here:
Chrome: ERR_BLOCKED_BY_XSS_AUDITOR details
(8 answers)
Closed 4 years ago.
I have one page where I submit hidden form where I store a large html code that is made in javascript and I want to send that code via form to another php file. It should work but my browser blocks it(google chrome):
<form action="napravi.php" method="post">
<input type="text" name="code" value=""/>
<input type="submit" id="forma_napravi"/>
</form>
and in javascript:
document.getElementById("forma_napravi").click();
and php page where I send the form data:
if(isset($_POST['code'])){
echo $_POST['code'];
}
and this is error that browser shows to me:
It will show error if you send html or javascript code this way because it is google chrome's in-built design.
I tried your code it was working. But when I put some code in place of blank, e.g. value="<script>alert('123');</script>", I got the same error
You may use htmlentities() to parse the html to string. Then the code you send will have no effect at all.
give your form an id and check if it is been submitted. like this-
In your html file
<form action="napravi.php" method="post" id="form">
<input type="text" name="code" value=""/>
<input type="submit" id="forma_napravi"/>
</form>
In your javascript
document.getElementById("form").on('submit', function(){
// your code
});
Hope this will help

Redirect from PHP file to HTML file? [duplicate]

This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
PHP fopen for writing fails
(2 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
So i have a contact form on my website. However when i submit it redirects me from my html page to the PHP page, id like to automatically redirect back. I have tried using HTACCESS but it redirects straight away and the PHP script doesnt run.
HTML FORM
<form action="action.php" method="post" class="contactForm" target="_top">
<input type="name" name="field1" placeholder="Enter your name..."><br>
<input type="email" name="field2" placeholder="Enter your email address..."><br>
<input type="text" name="field3" placeholder="Enter your message..."><br>
<input type="submit" value="Submit" id="button">
</form>
action.php
<?php
$path = 'data.txt';
if (isset($_POST['field1']) && isset($_POST['field2'])&& isset($_POST['field3'])) {
$fh = fopen($path,"a+");
$string = $_POST['field1'].' - '.$_POST['field2'].' - '.$_POST['field3'];
fwrite($fh,$string);
fclose($fh);
}?>
If i am going about this the wrong way please let me know, i am a complete beginner to PHP so i know very little.
Thanks.
header('Location: index.html');
http://php.net/manual/en/function.header.php
You can't output anything before the redirect.
A potentially better way to do this is not to post to the PHP script only to have it redirect back to html, but to post to it through ajax.
There are a few ways to do that. I assume you want to take the values from the Form and process them in PHP.
Using Ajax - With Ajax you can send data from your form to a PHP-Script without reloading the page. So in your case no redirect is needed See here for a small tutorial http://blog.teamtreehouse.com/create-ajax-contact-form
Combine PHP and HTML - You can input all your HTML Code into the PHP File and call the PHP File. See -> https://www.ntchosting.com/encyclopedia/scripting-and-programming/php/php-in/
ob_end_clean( );
header("Location: example.com");
exit();
Allows you to output something before redirecting.

How to make a BitCoin API call using PHP? [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
I am making a website that gets information about a bitcoin wallet balance. What I am trying to achieve is for whatever the persons text input is to be sent to the link but sent like "http://link.com/ + input"
I am new to PHP so I am not sure what to do next or what I have done wrong so far.
<?php
echo ' <form action="https://blockchain.info/q/addressbalance/" method="post">
BTC Address: <input type="text" name="address"><br>
<input type="submit" value="Submit">
</form>'
$input = $_POST['post'];
echo $input
?>
Old Answer:
Use string concatenation.
For example:
$link = "http://link.com" . $input;
Update:
It seems like what you actually want to do is to redirect the user based on their input. For this, you should use the header() function.
Here is an example as to how you can use the function for redirection purposes.
header('Location: ' . $link);
die();
Note that this must go at the very top of the page, before any HTML is outputted. (So of course, your code for $link should go before it as well)
As for your <form action="https://blockchain.info/q/addressbalance/" method="post">, you will want to change the action to the current document. This is because $_POST['post'] only receives data after you pressed submit. Changing it to action="" generally defaults to this behavior.

Categories