PHP downloading instead of running [duplicate] - php

This question already has answers here:
Apache is downloading php files instead of displaying them
(27 answers)
Closed 1 year ago.
I am working on visual studio code making a few web pages and I need to send html form data from one to another. I am using the get method but when I submit the form the PHP file downloads instead of running, is there a way to fix this?
<form action="login.php" method="get">
<p class="normal" id="samelinechild">Dora the</p>
<input name="verification" id="very" type="text">
<p class="normal"id="samelinechild">explorer</p>
<input type="submit">
</form>

If you send data use POST method not GET. I don't know if this will fix the problem but it's the rule.

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

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.

Ajax $.post not working in Firefox [duplicate]

This question already has answers here:
Send ajax request
(2 answers)
Closed 5 years ago.
I read a lot questions and answers about this problem, but noone solved mine.
So i have a simple form:
<form action="#" name="pm" id="pm" method="post">
<input onclick="sendPM()" name="pmSubmit" type="submit" value="Send" />
</form>
And the following function:
function sendPM() {
$.post("something.php").then(function(data) {
});
}
In "something.php" i doing a simple INSERT INTO to my database.
This code works perfectly in IE but dont work in Firefox.
I dont know, what can i do to solve this.
Pls if anyone can help me, will save a lot of time for me.
This sounds like magic, but fixed it. Change
onclick="sendPM()"
to
onclick="sendPM(); return false"
I don't know why, but now both Chrome and Firefox perform the ajax call. Previously firefox did not even make the connection to the server...

All of my PHP code coming as comments in HTML [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 6 years ago.
This is my code where I intend to handle the PHP form data on the same page.
However the PHP script doesn't get executed and instead all PHP code is rendered as comments when inspected.
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php // php code ?>
//html code
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<br><br><br>
<input type="text" required name="username" placeholder="Username" class="forminput">
<br>
<span><?php echo $nameErr; ?></span>
<br>
<input type="submit" value="Sign Up" class="forminput">
</body>
</html>
How can I execute the php script in browser? All files are in root directory.
You possibly need a XAMPP server to execute your php code even the file extension you have given as .html.
If your file contain the php code it will need a web server(XAMPP) to execute the process.
Hope this tutorial will help you out with Use XAMPP to run php program
You can't execute PHP code in the browser. You need a server that can handle PHP. I suggest you start with an all in one LAMP stack that supports PHP out of the box.
Try this one:
XAMPP

Categories