PHP form action not redirecting - php

I developed a web crawler to search for certain tags on my companies websites to make sure they are live, have Google analytics, blah blah. However, my company has close to a hundred websites so the actual crawl process, is literally a crawl. So I wanted to create a form where the user inputs a web address of one of our companies and it only crawls that one website. I am not good with forms, so what I basically want the form to do is store the url the user inputs then redirect to a different page where the url is given to the crawler and the results are shown.
Here is basically what I have so far, not much, I am having trouble redirecting to a different page and storing the URL variable so I can pass it to the crawler code that I have.
<div id="main-content" class="mc-left"> <div class="entry"> <div style="position:absolute; margin-left:520px; height:25px; width:120px; font-size:10px;"> </div>
</div>
<h2><?php the_title(); ?></h2>
<form name="form1" id="form1" method="POST" action="submitcrawler.php">
<div class="hiddenfields">
<p>Website Address:<br>
<input name="websiteaddress" type="text"></p>
<input type="submit" class="submit" name="submit" value="Submit">
</form>
As you can see I want this form to bring me to submitcrawler.php, however, when I create that php file, when I hit submit it brings me to the current slug (../crawler-2/submitcrawler.php instead of ../submitcrawler.php) so it throws up a 404 error

The form is submitting to 'submitcrawler.php' in the same folder as the file that you're looking at, so if its in /crawler-2/ then that's where its looking.
Use ../ if you want to ascend to the directory above, or probably better, use / and enter the path to the file from the web root (the top directory viewable by apache / the web server).
So
<form action="../submitcrawler.php">
or
<form action="/submitcrawler.php">

For the functionality that you're looking for, you could try using method="GET". That way, you can see the information that is being passed to the other PHP script in the URL.
Then simply retrieve the information in the other PHP script:
if(isset($_GET['websiteaddress'])) {
$websiteaddress = $_GET['websiteaddress'];
} else {
echo "No web address was received.";
}
In terms of the form action attribute, you need to use an absolute path if the scripts will both be static, otherwise if the scripts are dynamic and may change locations on the servers, then use relative paths.
Path Info:
http://en.wikipedia.org/wiki/Path_%28computing%29
http://webdesign.about.com/od/beginningtutorials/a/aa040502a.htm

If the file you're talking about is under crawler-2 directory it will submit the form to that file unless you use a relative path ../submitcrawler.php

The action you have set on that form will send it to submitcrawler.php in the same directory as the current script. Try changing the action to ../submitcrawler.php, or alternatively set it to the absolute url of the script (http://mydomain.me/submitcrawler.php)

You used a relative path in your post action value. If for example your crawler script is in your webroot you should use action="/submitcrawler.php". If not you can do something like action="/path/to/submitcrawler.php"

Related

PHP - Absolute & Relative URL Types

I wrote a login code in PHP:
<form NAME="form1" METHOD="POST" ACTION="operation/validateLogin.php">
Username <br/><input name="username" type=text autocomplete="off"><br/><br/>
Password <br/><input name="password" type=text autocomplete="off"><br/><br/>
<button class="btn btn-primary submit" type="submit">Sign In</button>
</form>
When I submit the form the credentials are sent to a validation file. If an error occurs the file sends the error message back to the login page:
header("Location: http://localhost/demoapp/login.php/?em=28");
I handle the 'GET' parameter and print the error message:
if (isset($_GET['em'])){
if($_GET['em'] == 28){$errorMessage = "Your username or password was incorrect.";}
}
Now the user needs to try to login again by resubmitting the form, but the action of the form is:
operation/validateLogin.php
and the URL is now:
http://localhost/demoapp/login.php/?em=28
Therefore, when the form is submitted the url becomes:
http://localhost/demoapp/login.php/operation/validateLogin.php
When it should be...
http://localhost/demoapp/operation/validateLogin.php
How do you prevent this from happening to the URL?
The ACTION attribute of an HTML form can be set with a relative URL:
/operation/validateLogin.php
or
/validateLogin.php
It's actually recommended to work with relative URLs for HTML elements:
Absolute vs relative URLs
However, when working with PHP an absolute URL is your best option:
http://localhost/demoAPP/operation/validateLogin.php
The use of absolute URLs will relieve your code of accidental URL concatenation.
I had trouble recently figuring out which type of URL to use for certain situations, but this is what I've realized...
PHP (local/server language) = Absolute Local/Server Address
require "C:/dev/www/DEMO/operation/login/validateLogin.php";
include "C:/dev/www/DEMO/operation/login/validateLogin.php";
header("Location: http://localhost/demoapp/login.php/?em=28"); (redirect to a web address)
This may seem really simple but remembering this will save you a lot of troubleshooting time.
If you are using .PHP files, alter the URL in any way, and are not using absolute URLs you will most certainly receive errors.
Additional: You'll notice that you can use a web address for HTML attributes and not run into any problems. However, with PHP requires and includes you can only use local addresses. There is a reason for this limitation and it's all because of one important PHP setting...
https://help.dreamhost.com/hc/en-us/articles/214205688-allow-url-include

Specify upload path using swfupload

I'm using SwfUpload and I want to be able to specify the destination folder. Right now I have hard coded the final destination, but I would like to be able to control it on the fly. Is there a way to get SwfUpload to submit it to my upload script? I tried adding it as a hidden variable to the form that displays the uploader, but it doesn't make it to my script:
Here's the form that shows the uploader. The rest of the page is pretty standard so I didn't include it. Look for the hidden field "destinationpath"
<form id="form1" action="nowhere.html" method="post" enctype="multipart/form-data">
<p>Select file to upload for program.
</p>
<div class="fieldset flash" id="fsUploadProgress">
<span class="legend">Upload Queue</span>
</div>
<div id="divStatus">0 Files Uploaded</div>
<div>
<span id="spanButtonPlaceHolder"></span>
<input id="btnCancel" type="button" value="Cancel All Uploads" onclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px; font-size: 8pt; height: 29px;" />
</div>
<input name="destinationpath" type="hidden" value="c:/DigitalMediaFiles/">
</form>
Then in upload.php I check for it:
if (isset($_REQUEST["destinationpath"]))
$save_path = $_REQUEST["destinationpath"];
but its not set. I suspect that swf upload does not submit the form.
The way to do it is to specify the parameter in post_params of the SWFSettings object
post_params:
{
"destinationpath" : "C:\DigitalMediaFiles"
},
I tried using addPostParam() but that did not work for me. putting it in post_params of the settings did.
If your destination folder is not related to the up-loader(client), you can do it in your php script, without getting that variables from client side scripts. That is save the file in your target directory by php script.
If the destination folder is different from each client, still you can save them in a same directory and use your .htaccess file to rewrite urls for each client(using client id). In this case you need to save your file that keeps client id in a portion of the file name. You can explode file name by using a delimiter.
If still you need to do it your own way that is you mentioned in your question,just use post_params: {} function in your configuration js file which is coded for the swfupload.js file. In that case post_params: {} function will look like the following:
post_poarams: { 'your-variable-name-in-php-goes-here': 'value of this variable at this time goes here' },

How to Send Form Data to an External Database and Process It?

In one site, www.example.com, I have a form. It is processed and stored in its database.
Is it possible to send this form data also to another site (www.client-site.com)? It is located on a different server. And this client-site should receive the data and store it on its own database.
I am not sure of the terminology and what should I've been looking for.
Tested different search queries here in SO and this is what most resembles it:
[php] [mysql] +form +"$_post" +external
I'd like to develop this with PHP and the databases run MySQL, and surely security is important in this data transaction.
And hoping this is not a SOAP matter... ;)
Justification:
www.example.com runs a mini site of one of my clients
www.client-site.com is the client main site
being a large company, they cannot provide me credentials to their main site database
I have to propose an alternate solution
You will need to have something like a webservice (this is the word you are looking for) on site b which you can use from the code within your site a.
SOAP is one possibility to create a webservice, but there are many other possibilities. One is shown in this answer on stackoverflow.
NEVER EVER try to archiv this using forms and something like cURL!
Further you should look for proper authorization on your endpoint (site b) and ensure that ssl is used, as security is important to you.
If you have a form hosted on www.example.com like this:
<form method="post" action="http://www.client-site.com/handler.php">
...
Then the page http://www.client-site.com/handler.php will be able to access the post variables.
This is why it is important that you validate your post data in your own PHP applications as you can never be sure where the data is coming from and therefore cannot trust it.
I can think of a very ugly solution wich will do the trick :)
<script language="JavaScript">
function submitForm(theform) {
theform.action = "SITE ONE";
theform.target="myframe1";
theform.submit();
theform.target="";
theform.action = "SITE2";
theform.submit();
}
</script>
<html>
<body>
<form action="" onSubmit="submitForm(this); return false;">
<input type="text" name="userName" value="" />
<input type="Submit" />
</form>
<IFRAME name="myframe1" src="about:blank" width="0"
height="0"></IFRAME>
</body>
</html>
This is not so conventional but will do the trick !
check it out :)

How can an HTML page extract parameters passed through a form?

My question is should I convert two html pages to php pages, so the called page can access its POSTed parameters, or is there a way for a called html (.html extension) page to access posted parameters?
I've been reading that because posted parameters are server-side there is no way for JavaScript to do this being client side. I've seen nothing about one html page accessing
parameters if that .html page was accessed via a POST.
Here is my calling form. The called form, needs access to TransDesc (below), which is a text field.
<script type="text/javascript" language="JavaScript1.2">
// Check where we came from so that we can go to the right spot when the
// form gets posted from outside of our HTML tree.
var PostURL = '/event.html';
</script>
Enter a Donation Amount
<script type="text/javascript" language="JavaScript1.2">
document.write(
'<form name="InvGenPayDonation"
action="'+PostURL+'"
onsubmit="return validateForm();"
method=POST>');
</script>
<p> $ <input type='text' name='Amount' value="0.00">
</p>
<p>In honor of <span style="color: #FF0000">
<input type='text' name='TransDesc' id='TransDesc' value="" >
</p>
<input type="submit" value="Next"> <br /><br />
A static HTML file cannot access variables that have been POST'ed to it. It can't even know they're there as they're sent to the server in the HTTP request, the server then deals with them and sends the HTML page in the HTTP response. They're 'consumed' before the page is even sent to the client.
You could use GET and access them via JavaScript, or configure Apache to server .html files as PHP files instead though.
In my opinion, php is the easiest way to go, and as far as languages go is pretty easy to learn and pretty intuitive.
You'll have to either convert them to PHP or use GET instead of POST, as GET parameters are accessible through window.location.href
Yes, I would recommend converting the pages to php. If you are set on using HTML files you will have to edit your htaccess file to run HTML pages as php.
You can always use ajax to retrieve and send post and get values.
You can retrieve it with js by creating a php file and access those with ajax from your html files.

HTML: Forms and Subdomains

I have a very basic question. Please consider a site with subdomains:
MAIN SITE: www.domain.com
Sub-domain 1: sub1.domain.com
Sub-domain 2: sub2.domain.com
The main site, in http://www.domain.com/index.php, has a form:
<form action="http://sub1.domain.com" method="post">
<input type="text" name="userInput">
<input type="submit">
</form>
Will the data be posted to http://sub1.domain.com and be accessible by the following: (?)
<?php
$input = $_POST['input']
//the goal is to make $input, a variable in http://sub1.domain.com/index.php
//equal to what the user submitted in the form at http://www.domain.com/index.php
?>
I simply need to load sub1.domain.com/index.php with the posted variables present, no real-time fanciness. From my reading, I've determined there may be a setting which blocks this activity in apache or some other application, but theoretically, data should be able to be sent to any domain/subdomain from any domain/subdomain using POST, correct?
**> Step1: Go to Cpanel select Subdomain icon and click on manage
redirection link of your subdomain. and set redirection to
http://sub1.domain.com/xyz.php and save setting
Step 2: Changes in form as below
Step 3: Changed in xyz.php which must be located in inside sub1
directory
sub1 | |__xyz.php
xyx.php file
**

Categories