How to run php on a button click - php

So I have been browsing trough a lot of threads, and the only suitable solution for my situation I found was this:
PHP:
<?php
if(isset($_POST["submit"])) {
my php code goes here
}
?>
HTML:
<form id="usp-form-23" class="usp-form" method="post" enctype="multipart/form-data" action="" data-validate="parsley" data-persist="garlic" novalidate>
<input name="usp-title" type="text" value="" data-required="true" required="required" maxlength="99999" placeholder="Answer" class="usp-input usp-input-title" />
<input name="submit" id="submit" type="submit" class="usp-submit usp-submit-default" value="Send Message" />
</form>
But it doesn't work.
Maybe I'm missing something?
Need help.
P.S. I can't use an external php file. Only php code inside the same file, because of the variables I need to grab from the post.
I also can't use $_GET, because if a person refreshes the page, my php will be repeated, which I don't want. I only need to run my php once.

Related

Can I add a .php file URL to Blogger?

I would like to know if there's a way to add a .php file to a Blogger template using a direct link. I am unsure whether or not I can add it to my form action with a URL as Blogger does not support .php directly.
<form method="post" action="CAN_I_USE_A_URL_HERE?.php">
<input name="Name" placeholder="Name*:"/>
<input name="E-mail" type="email" placeholder="E-mail*:"/>
<input name="Phone" type="email" placeholder="Phone:"/>
<label>Write your message!</label>
<textarea name="Message"></textarea>
<input id="submit" name="submit" type="submit" value="submit"/>
</form>
Blogger will not run server-side code.
You need to put your PHP code on some server that will run it for you, then make the <form> point to that.

PHP not process data from multipart/form-data forms

My website does no process data from forms with the enctype="multipart/form-data" set. However the 'php://input' gets the data even if the php manual says it shouldn't be available for the enctype. I think it might be some settings that is wrong but I have no idea which it might be.
Some code:
<?php
var_dump($_REQUEST);
echo file_get_contents("php://input");
?>
<form id="slideForm" action="" method="post" enctype="multipart/form-data">
<input type="text" name="test">
<input type="submit" name="submit" id="submit" value="ADD"/>
</form>
Update
I have been in contact with the support of the company that host the servers for my website and we have solved the problem. I'm not completly sure on what is the problem but it was somethin funky with their servers and php. I don't work on PHP 7.0 or PHP 5.6 but if I use their native (PHP 5.5) it works without problems.
I don't understand which type of data you want to POST. If it's just a test you should only have:
<?php
var_dump($_POST);
?>
<form id="slideForm" action="" method="post">
<input type="text" name="test">
<input type="submit" name="submit" id="submit" value="ADD"/>
</form>
If you want to POST a file you must have:
<?php
var_dump($_POST);
?>
<form id="slideForm" action="" method="post" enctype="multipart/form-data">
<input type="file" name="test">
<input type="submit" name="submit" id="submit" value="ADD"/>
</form>
Make sure you are able to POST data on your php server.
Check these php variables:
upload_max_filesize
upload_tmp_dir
file_uploads
In most cases you will not need to use this attribute at all. The default value (i.e. if you don't use this attribute at all) is "application/x-www-form-urlencoded", which is sufficient for almost any kind of form data. The one exception is if you want to do file uploads. In that case you should use "multipart/form-data".
Try the following code for echo "test" data.
<?php
var_dump($_REQUEST);
echo $_REQUEST['test'];
?>
<form id="slideForm" action="" method="post">
<input type="text" name="test">
<input type="submit" name="submit" id="submit" value="ADD"/>
</form>
I tried your code, and it is working as expected:
the $_REQUEST is populated properly and the php-input is empty.
Considering that your code looks fine, and it is working as expected on my server, i suggest you check your .htacces (or equivalent) for filter/rewrite modules, maybe even server config settings or even page encoding.
99% it's not the code itself.
<?php
// File data will display here
print_r($_FILES);
// OR you can write with condition
if($_FILES['test']){
print_r($_FILES);
}
?>
<form id="slideForm" action="" method="post" enctype="multipart/form-data">enter code here
<input type="file" name="test">
<input type="submit" name="submit" id="submit" value="ADD"/>
</form>
When you send the header Content-Type:multipart/form-data; PHP parses the data from request into $_POST and $_FILES and clears the input buffer. In order to read the input buffer you should remove that header.

HTML/PHP : how can I have multiple form that send informations to the same page?

My problem is this :
I got 2 forms that are supposed to send different informations to the same page (via POST method). Each form has a submit button. However when I press any of the button, information from both forms are sent to the page.
Is it normal or is there something that I do wrong ?
I can already tell you without looking at your HTML.
You have to properly close the first form before opening the second. Easy mistake to make.
<form method="post" action="page.php">
<input type="text" name="something" />
<input type="submit" value="Submit" />
</form>
<form method="post" action="page.php">
<input type="text" name="somethingelse" />
<input type="submit" value="Submit" />
</form>

User input not showing in search form

I have a free form search that I moved from my main content to my header. Once I moved the form into the header the text inside of it stopped showing. The placeholder works but when you begin typing it is completely blank. The search and the functionality work perfectly, I just cannot see what I am typing. Any ideas?
<form class="search" action="/all-results/" method="get">
<fieldset>
<span class="text"><input name="searchProducts" value="All" type="hidden">
<input type="hidden" name="search_selection" value="all">
<input name="byString" id="s" type="text" value="" placeholder="<?php echo __('Search','Avada'); ?>" /></span>
</fieldset>
<form name="input" action="/all-results/" method="get">
<input type="submit" id="search_button" value="Search">
</form>
</form>
Your HTML is invalid. You can not nest a <form> element inside another <form>. Messing up forms and tables are two of the best ways to produces peculiar browser behaviour, so my guess is that this is your problem.
If you correct your HTML (validate it!) and the problem persists, then post a minimal HTML page with CSS so that there is something for us to debug.

Facebook iframe - how to send form (POST method)?

I have very simple form (the file is called message.php):
<?php
print_r($_POST);
?>
<form method="post" target="_top" action="<?php echo CANVAS_URL;?>message.php">
<input type="text" name="your_name" />
<input type="hidden" name="signed_request" value="<?php echo $_REQUEST['signed_request'];?>" />
<input type="submit" name="send" />
</form>
I found one solution of this issue - put into the form hidden input with the signed_request - I did it but unfortunately I am still facing with this problem -- I cannot retrieve sent POST data.
If I change the method to method="get", everything is working well, but I would need to data from POST.
Could anyone help me, how to solve this problem? Thanks!
Try this. I don't believe you need to use target in FB canvas aps anymore. Also a form ID would be good.
<form method="POST" id="my_form" action="message.php">
<input type="text" name="your_name" />
<input type="hidden" value="<?php print $_POST["signed_request"] ?>" name="signed_request" />
<input type="submit" name="submit" />
</form>
POSTing to Canvas URLs (as in http://apps.facebook.com/namespace) is simply not supported.
But why post to the top window instead of simply staying within the iframe? It's way better as it doesn't require the entire page to be reloaded, only the iframe.

Categories