Jump to content

[SOLVED] 2 actions with 1 submit button...


TDK

Recommended Posts

I am using a script that checks PageRank and Backlinks and I want to log the URL's submitted and then continue with the operation.  I have put in the write to .txt file code in the middle there but it doesn't write the URL to the .txt file

 

Here is what I have:

 

<form method="post" action="./" onsubmit="getResults(this.url.value); return false;">
<input type="text" name="url" value="<?php echo ($_GET['url']) ? htmlspecialchars($_GET['url']) : 'http://edirectories.info/'; ?>" size="40" />
<input type="submit" value="Submit" class="formbutton"/>
</form>
<div id="results">
</div>
<?php
$log_file = "url.txt"; 
$url = $_GET['url']; 
$fp = fopen("$log_file", "a"); 
fputs($fp, "$url\n"); 
flock($fp, 3); 
fclose($fp);
?>
<?php if (isset($_GET['url'])) { ?>
	<script language="javascript" type="text/javascript">
		window.onload = function () {
			getResults('<?php echo $_GET['url']; ?>');

		}
	</script>
<?php } ?>

 

Where it has $url = $_GET['url'];  --- If I put "TEST" in replace of $_GET['url'] it will correctly log the text "Test" to the "url.txt" but otherwise, I assume it tries to enter empty text.  Can anyone show me simply how to get it to log the URL submitted from the textbox into the url.txt and then continue with normal operation?  The website this script is working on is: http://edirectories.info/pagerank/index.php

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/76273-solved-2-actions-with-1-submit-button/
Share on other sites

<form method="post" action="<?=$_SERVER[php_SELF]?>">
<input type="text" name="url" value="<?php echo ($_GET['url']) ? htmlspecialchars($_GET['url']) : 'http://edirectories.info/'; ?>" size="40" />
<input type="submit" value="Submit" class="formbutton"/>
</form>
<div id="results">
</div>

<?php
if($_POST)
{
$log_file = "url.txt"; 
$url = $_POST['url'];

$fp = fopen("$log_file", "a"); 
fputs($fp, "$url\r\n"); 
flock($fp, 3); 
fclose($fp);

if (isset($_GET['url'])) { ?>

	<script language="javascript" type="text/javascript">
		window.onload = function () {
			getResults('<?php echo $_GET['url']; ?>');

		}
	</script>

<?php 
} 
}
?>

do you also have javascript somewhere else in the file .. that calls the getResults function?

do you also have javascript somewhere else in the file .. that calls the getResults function?

 

Yes, this Javascript file is called before the code I originally posted:

<script language="javascript" type="text/javascript" src="./js/main.js"></script>

 

It contains the following code which calls the getResults.php file:

 

var xml = makeXML();
var el = new Array();
var options = new Array();
options['pagerank'] = 'PageRank';
options['alexaRank'] = 'Alexa Rank';
options['dmoz'] = 'Listed in DMOZ';
options['backlinksGoogle'] = 'Backlinks from Google';
options['backlinksYahoo'] = 'Backlinks from Yahoo';
options['backlinksMSN'] = 'Backlinks from MSN';
options['resultsAltaVista'] = 'Results from AltaVista';
options['resultsAllTheWeb'] = 'Results from All the Web';
options['thumbnail'] = 'Website Thumbnail';
function makeXML () {
if (typeof XMLHttpRequest == 'undefined') {
	objects = Array(
		'Microsoft.XmlHttp',
		'MSXML2.XmlHttp',
		'MSXML2.XmlHttp.3.0',
		'MSXML2.XmlHttp.4.0',
		'MSXML2.XmlHttp.5.0'
	);
	for (i in objects) {
		try {
			return new ActiveXObject(objects[i]);
		} catch (e) {}
	}
} else {
	return new XMLHttpRequest();
}
}
function get (id) {
return document.getElementById(id);
}
function getResults (url) {
el['results'] = get('results');
xml.open('get', 'getResults.php?url=' + window.encodeURI(url));
xml.onreadystatechange = function () {
	if (xml.readyState == 4) {
		el['results'].innerHTML = '';
		if (window.ActiveXObject) {
			doc = new ActiveXObject('Microsoft.XMLDOM');
			doc.async = 'false';
			doc.loadXML(xml.responseText);
		} else {
			parser = new DOMParser();
			doc = parser.parseFromString(xml.responseText,'text/xml');
		}
		xmlDoc = doc.documentElement;
		x = 0;
		for (i in options) {
			x++;
			title = options[i];
			value = xmlDoc.getElementsByTagName(i)[0].childNodes[0].nodeValue;
			if (i == 'thumbnail') {
				value = '<img src="' + value + '" alt="Thumbnail Not Available" />';
			}
			if (i == 'dmoz') {
				if (value == '1') {
					value = 'Yes';
				} else {
					value = 'No';
				}
			}
			style = (x % 2) ? ' style="background-color: #EEEEEE"' : '';
			el['results'].innerHTML += '<div' + style + '><span>' + value + '</span>' + title + '<b class="clear"></b></div>';
		}
	} else {
		el['results'].innerHTML = '<div>Loading...</div>';
	}
}
xml.send(null);
}

 

Here is the code in getResults.php:

 

<?php
include 'php/classes/pagerank.class.php';
$options = array(
	'pagerank' => true,
	'alexaRank' => true,
	'dmoz' => true,
	'backlinksGoogle' => true,
	'backlinksYahoo' => true,
	'backlinksMSN' => true,
	'resultsAltaVista' => true,
	'resultsAllTheWeb' => true,
	'thumbnail' => true
);
new pagerank(urldecode($_GET['url']), $options);

?>

 

Although, I don't think that has anything to do with logging the URL in the textbox. 

 

I will test the POST option...

Ok i rewrote the code to work with the javascript since the other script reads the variable through GET

 

<form method="GET" action="<?=$_SERVER[php_SELF]?>">
<input type="text" name="url" value="<?php echo ($_GET['url']) ? htmlspecialchars($_GET['url']) : 'http://edirectories.info/'; ?>" size="40" />
<input type="submit" value="Submit" class="formbutton"/>
</form>
<div id="results">
</div>

<?php
if($_GET)
{
$log_file = "url.txt"; 
$url = $_GET['url'];

$fp = fopen("$log_file", "a"); 
fputs($fp, "$url\r\n"); 
flock($fp, 3); 
fclose($fp);

if (isset($_GET['url'])) { ?>

	<script language="javascript" type="text/javascript">
			getResults('<?php echo $_GET['url']; ?>');
	</script>

<?php 
} 
echo "DONE";
}
?>

Brilliant!  Thank you so much!

 

It is logging the submitted URL's correctly.  I may be back if I run into problems removing duplicate entries in the .txt file, displaying/logging only the last 10-20 entries, etc. - LOL

 

 

Thanks again!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.