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

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

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

How to post HTML FORM Data to a http link given to you by a client using php

Hi guys im quite newbie on coding but i want to ask How to post HTML FORM Data to a http link given to you by a client thanks.
Try creating a .html file as below:
<html>
<form method="post" action="urclientlink">
<input type="text" name="firstname"/>
<input type="text" name="lastname"/>
<input type="text" name="submitButton" value="Submit Form"/>
</form>
</html>
Please elaborate on your question for a better answer. Thanks!
This is no different from submitting form data to a page on your own site. You simply amend the form's action attribute:
<form action="http://another-server.com/another-page">
You also need to be sure you get the right METHOD: e.g. GET or POST:
<form action="http://another-server.com/another-page" method="GET">
You will need to check with the client to see which page and method they need.

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.

html form on reloading page is not cleared on form send

I am working on a website that has a kind of questionare. It is a PHP script that reads the questions from a database and shows some content depending on your answers. The user will click an answer and the page will reload re-posting the previously entered form data so the already answered questions will show disabled buttons/radio forms with the answers checked.
I noticed during debugging that the previously sent stuff is not cleared and always reposted (the page works however, i just thought it looks unclean)
example:
?position=9&p_item1=4&p_item2=2&p_item3=4&p_item4=2&p_item5=2&p_item6=2&p_item7=2&p_item8=1&position=9&p_item1=4&p_item2=2&p_item3=4&p_item4=2&p_item5=2&p_item6=2&p_item7=2&p_item8=1&position=9&p_item1=4&p_item2=2&p_item3=4&p_item4=2&p_item5=2&p_item6=2&p_item7=2&p_item8=1&position=9&p_item1=4&p_item2=2&p_item3=4&p_item4=2&p_item5=2&p_item6=2&p_item7=2&p_item8=1&position=9&p_item1=4&p_item2=2&p_item3=4&p_item4=2&p_item5=2&p_item6=2&p_item7=2&p_item8=1&position=9&p_item1=4&p_item2=2&p_item3=4&p_item4=2&p_item5=2&p_item6=2&p_item7=2&p_item8=1&position=9&p_item1=4&p_item2=2&p_item3=4&p_item4=2&p_item5=2&p_item6=2&p_item7=2&p_item8=1&position=9&p_item1=4&p_item2=2&p_item3=4&p_item4=2&p_item5=2&p_item6=2&p_item7=2&p_item8=1&p_item9=2&position=9&p_item1=4&p_item2=2&p_item3=4&p_item4=2&p_item5=2&p_item6=2&p_item7=2&p_item8=1#lastrow
how can i get rid of the post/put data getting exponentially bigger?
I am assuming that the "answer" you are referring to is a link. If so, you can easily use the $_POST superglobal to send the data:
<!-- javascript that creates the post() function-->
<script>
function post(event) {
event.preventDefault();
document.getElementById("my_form").submit();
}
</script>
<!-- form with the information you want to post-->
<form action="action.php" method="post" id="my_form" style="display: none;">
<?php
/*You'll have to create the needed php code that will echo the
hidden inputs for the form that should look like this:
<input type="hidden" name="position" value="9" />
<input type="hidden" name="p_item1" value="4" />
... and so on.*/
?>
</form>
Your Link

use GET and POST in the same form [duplicate]

This question already has answers here:
Post and get at the same time in php
(6 answers)
Closed 9 years ago.
Regards.
I want to send some of my values in the form by GET method and some by POST method. How can i do it ?
Here is an example. I wan to make this kind of page, but i want a single submit button for both the forms. Is it possible to use GET method on some feilds and POST method on some ?
<html>
<form name="f2" action="a.php" method="POST">
Name: <input type="text" name ="uname"/><br>
Password: <input type="password" name ="pass"/><br>
<input type="submit" >
</form>
<br><br>
<form name="f1" action="a.php" method="GET">
Fav Movie: <input type="text" name="favmovie"><br>
Movie rating:<input type="text" name="rating"><br>
<input type="submit" value="My FAv movie">
</form>
</html>
HttpRequests do not allow for multiple request types, however you could do
<form method="post" action="answer.php?foo=bar">
You cannot send the values of some input fields via GET and others via POST with PHP and HTML.
If you add JavaScript, you could add the values of some input fields to the query string of the action-attribute of the form. But this seems to be too much of a hassle.
For same submit button -
It will be only possible if you start your form tag with conditional Programming.
i.e. after rendering from web server it will show only one form tag for a specific submit button.
Hope this helps you.
The first question is, why would you want to do that?
I think the simple answer is no, a form can only be submit via one method, by get or post

Categories