I want to show something and lock the screen while my PHP script is running in the background.
The script can take a long time to run because it is pulling in meta data for a list of URLs, so depending on how many URLs are entered into the text area it could take up to 5mins.
I want to use the BlockUI jQuery code but have no idea how to set it up in my php script could anyone help me please?
Here is my code:
<form method="get" action=<?php echo "'".$_SERVER['PHP_SELF']."'";?> >
<p>URL of Competitor:</p>
<textarea name="siteurl" rows="10" cols="50">
<?php //Check if the form has already been submitted and if this is the case, display the submitted content. If not, display 'http://'.
echo (isset($_GET['siteurl']))?htmlspecialchars($_GET['siteurl']):"http://";?>
</textarea><br>
<input type="submit" value="Submit">
</form>
<div id="nofloat"></div>
<table class="metadata" id="metatable_1">
<?php
ini_set( "display_errors", 0);
function parseUrl($url){
//Trim whitespace of the url to ensure proper checking.
$url = trim($url);
//Check if a protocol is specified at the beginning of the url. If it's not, prepend 'http://'.
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
//Check if '/' is present at the end of the url. If not, append '/'.
if (substr($url, -1)!=="/"){
$url .= "/";
}
//Return the processed url.
return $url;
}
//If the form was submitted
if(isset($_GET['siteurl'])){
//Put every new line as a new entry in the array
$urls = explode("\n",trim($_GET["siteurl"]));
//Iterate through urls
foreach ($urls as $url) {
//Parse the url to add 'http://' at the beginning or '/' at the end if not already there, to avoid errors with the get_meta_tags function
$url = parseUrl($url);
//Get the meta data for each url
$tags = get_meta_tags($url);
//Check to see if the description tag was present and adjust output accordingly
echo (isset($tags['description']))?"<tr><td>Description($url)</td> <td>".$tags['description']:"<tr><td>Description($url)</td><td>No Meta Description</td> </tr>.";
}
}
?>
</table>
<script type="text/javascript">
var exportTable1=new ExportHTMLTable('metatable_1');
</script>
<div>
<input type="button" onclick="exportTable1.exportToCSV()" value="Export to CSV"/>
<input type="button" onclick="exportTable1.exportToXML()" value="Export to XML"/>
</div>
And here is the link to the jQuery Block UI: http://jquery.malsup.com/block/#demos
Also will I need to include any files?
It'll be great if someone could point me in the right direction :)
Thanks very much!
Ricky
Put the php in a separate file, then call that script using ajax and return the results to the page once retrieved??
Related
I have couple of identical HTML pages which take user input and with PHP I save the input on text files. As it's always the same I would like to apply the same PHP on every html page. So my question is if there is a way to redirect to the next html page but not through PHP, so that the PHP can be reusable for all the pages?
Because if I add onclick="window.location.href='/main2.html' then the PHP is not fired up or if I change action="main2.html" then again the PHP is not included. Or if I add header("Location: main2.html"); then it cannot really be applied to all the html pages, as it goes main.html -> main2.html -> main3.html etc.
HTML 1:
<form method="post" action="process.php">
<input type="text" name="address" required>
<input type="submit" value="Submit" id="intro">
</form>
HTML 2:
<form method="post" action="process.php">
<input type="text" name="address" required>
<input type="submit" value="Submit" >
</form>
PHP:
<?php
$myfile = fopen("text.txt", "a+");
$address = $_POST['address'].";";
fwrite($myfile, $address);
//header("Location: main2.html");
fclose($myfile);
?>
Thank you in advance!
Although you don't want to use PHP, it can make this pretty simple for you. You can use the session variable in PHP to track the user, the entries he has made and even allow them to go back and forward.
You can read more on it here.
You can redirect the user based on the session counter or the source from where the call is made and direct them accordingly to the next page. This makes your code scalable as well.
So you code will be something like this:
session_start();
if( isset( $_SESSION['counter'] ) ) {
$_SESSION['counter'] += 1;
}else {
$_SESSION['counter'] = 1;
}
$newURL = "HTMLPage".$_SESSION['counter']."html";
header('Location: '.$newURL);
The above code will redirect the user to the next page. PS: You will have to handle the case for your last html page.
I am using a form and the 'get' method to offer users a return option to an unknown url that they came from within my site, as per the code below. I prefer this to just the browsers back button and it works without javascript.
The problem I am having is that some browsers (chrome, safari, there may be others) are adding a question mark to the end of the url they are referred back to. I don't want this for seo reasons.
My question is either:
1) Can I prevent the question mark within my php code somehow; or
2) Please could somebody show me how to redirect the url using htaccess, I potentially have urls that could end:-
.html?
.htm?
.php?
/?
Thanks in advance.
<?php
if (isset ($_SERVER['HTTP_REFERER']) ) {
$url = htmlspecialchars($_SERVER['HTTP_REFERER']);
echo '<br /><form action="' . $url . '" method="get">
<div id="submit"><input type="submit" value="Return to previous page" /></div>
</form>';
}
?>
The ? probably gets added because you're doing a GET request from a form.
Why not do something like:
<input type="button" onclick='document.location.href=<?php echo json_encode($url);?>'>;
use POST method instead of GET
Is it pertinent that you use a form for this? Why not use a hyperlink and style it to look however you want with CSS?
You could use try using a button instead of creating an input value that will be passed.
<form action="url" method="get">
<button type="submit">Return to previous page</button>
</form>
Instead of posting to random URLs (which is not really a good idea) consider using redirects
A solution would be
<?php
if (isset ($_SERVER['HTTP_REFERER']) ) {
$url = htmlspecialchars($_SERVER['HTTP_REFERER'],ENT_QUOTES,'UTF-8');
echo '<br /><form action="redirect.php" method="get">
<input type="hidden" name="return" value="'.$url.'">
<div id="submit"><input type="submit" value="Return to previous page" /></div>
</form>';
}
?>
then in redirect.php
<?php
if (isset ($_GET['return'] ) {
$url = $_GET['return'];
header('Location: '.$url, true, 303); // or 302 for compatibility reasons
echo 'Continue';
exit;
}else{
echo 'Error, no redirect specified';
}
you can replace action="GET" and $_GET with action="POST" and $_POST and it will work the same.
or simply
if (isset ($_SERVER['HTTP_REFERER']) ) {
$url = htmlspecialchars($_SERVER['HTTP_REFERER'],ENT_QUOTES,'UTF-8');
echo 'Return to previous page';
}
both will still create a new history entry in the browser.
In the htaccess you should have something like this:
RewriteEngine on
RewriteRule ^myPage-([a-zA-Z0-9_]*).html myPHPfile.php?var=$1
By the above piece added to your htaccess, when a URL like myPage-whatever.html is called, it's just like calling myPHPfile.php?var=whatever
I have developed a site for a client and he wants to be able to edit a small part of the main page in a backend type of solution. So as a solution, I want to add a very basic editor (domain.com/backend/editor.php) that when you visit it, it will have a textfield with the code and a save button. The code that it will edit will be set to a TXT file.
I would presume that such thing would be easy to code in PHP but google didn't assist me this time so I am hoping that there might be someone here that would point me to the right direction. Note that I have no experience in PHP programming, only HTML and basic javascript so please be thorough in any reply that you provide.
You create a HTML form to edit the text-file's content. In case it get's submitted, you update the text-file (and redirect to the form again to prevent F5/Refresh warnings):
<?php
// configuration
$url = 'http://example.com/backend/editor.php';
$file = '/path/to/txt/file';
// check if form has been submitted
if (isset($_POST['text']))
{
// save the text contents
file_put_contents($file, $_POST['text']);
// redirect to form again
header(sprintf('Location: %s', $url));
printf('Moved.', htmlspecialchars($url));
exit();
}
// read the textfile
$text = file_get_contents($file);
?>
<!-- HTML form -->
<form action="" method="post">
<textarea name="text"><?php echo htmlspecialchars($text); ?></textarea>
<input type="submit" />
<input type="reset" />
</form>
To read the file:
<?php
$file = "pages/file.txt";
if(isset($_POST))
{
$postedHTML = $_POST['html']; // You want to make this more secure!
file_put_contents($file, $postedHTML);
}
?>
<form action="" method="post">
<?php
$content = file_get_contents($file);
echo "<textarea name='html'>" . htmlspecialchars($content) . "</textarea>";
?>
<input type="submit" value="Edit page" />
</form>
You're basically looking for a similar concept to that of a contact-form or alike.
Apply the same principles from a tutorial like this one and instead of emailing using mail check out the file functions from PHP.net.
What did you Google on then? php write file gives me a few million hits.
As in the manual for fwrite():
<?php
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
// the content of 'data.txt' is now 123 and not 23!
?>
But to be honest, you should first pick up a PHP book and start trying. You have posted no single requirement, other than that you want to post a textfield (textarea I mean?) to a TXT file. This will do:
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
$handle = fopen("home.txt", 'w') or die("Can't open file for writing.");
fwrite($fh, $_POST['textfield']);
fclose($fh);
echo "Content saved.";
}
else
{
// Print the form
?>
<form method="post">
<textarea name="textfield"></textarea>
<input type="submit" />
</form>
<?php
}
Note that this exactly matches your description. It doesn't read the file when printing the form (so every time you want to edit the text, you have to start from scratch), it does not check the input for anything (do you want the user to be able to post HTML?), it has no security check (everyone can access it and alter the file), and in no way it reads the file for display on the page you want.
First thing to do is capture the information, the simplest way to do this would be the use of a HTML Form with a TEXTAREA:
<form method='post' action='save.php'>
<textarea name='myTextArea'></textarea>
<button type='submit'>Go</button>
</form>
On 'save.php' (or wherever) you can easily see the information sent from the form:
<?php
echo $_POST['myTextArea']
?>
To actually create a file, take a look at the fopen/fwrite commands in PHP, another simplistic example:
<?php
$handle = fopen("myFile.txt","w");
fwrite($handle,$_POST['myTextArea'];
fclose($handle);
?>
WARNING: This is an extremely simplistic answer! You will perhaps want to protect your form and your file, or do some different things.... All the above will do is write EXACTLY what was posted in the form to a file. If you want to specify different filenames, overwrite, append, check for bad content/spam etc then you'll need to do more work.
If you have an editor that is publicly accessible and publishes content to a web page then spam protection is a DEFINITE requirement or you will come to regret it!
If you aren't interested in learning PHP then you should think about getting a professional developer to take care of any coding work for you!
I had a similar need so we created a client-friendly solution called stringmanager.com we use on all our projects and places where CMS is not effective.
From your side, you just need to tag string in the code, i.e. from:
echo "Text he wants to edit";
to:
echo _t("S_Texthewantstoedit");
stringmanager.com takes care about the rest. Your client can manage that particular text area in our online application and sync wherever he wants. Almost forgot to mention, it is completely free.
Can use this line of code :
<form action="" method="post">
<textarea id="test" name="test" style="width:100%; height:50%;"><? echo "$test"; ?></textarea>
<input type="submit" value="submit">
</form>
<?php
$file = "127.0.0.1/test.html";
$test = file_get_contents('1.jpg', 'a');
if (isset($_POST['test'])) {
file_put_contents($file, $_POST["test"]);
};
?>
<form action="" method="post">
<textarea id="test" name="test" style="width:100%; height:50%;"><? echo "$test"; ?></textarea>
<input type="submit" value="submit">
</form>
Haven't had time to finish it, simplest possible, will add more if wanted.
I have a textbox that allows users to search my website.
<form method="post" action="">
<input type="text" name="search" id="search" />
<input type="submit" name="submit" id="submit" value="Search" />
</form>
Upon the user clicking the "Search" button, how do create the following URL, and append the contents of the search box to end, whilst adding "_" in replace of any spaces?:
http://www.mysite.co.uk/find/this_is_my_search
Also, if somebody edits the search query in the URL, how do I update the search textbox to reflect this change, so the box contains the following
this is my search.
You need to handle that url using you 404 page. You can use $_SERVER['REQUEST_URI'] to get that path. For instance you could do this in your custom 404 page:
<?php
if (stripos($_SERVER['REQUEST_URI'], "/find/") == 0) //If url is yoursite.co.uk/find/
{
$searchterm = basename($_SERVER['REQUEST_URI']);
//Search for $searchterm
//Print the results
}
else
{
header("HTTP/1.0 404 Not Found");
}
?>
You can change action URL with JavaScript before submit event.
You can Use onSubmit event or use jquery
$('form').submit(function(){
$(this).attr('action', $('#search').val().replace(' ', '_'))
})
Change form method to get.. And also if you're using code igniter or other framworks you should be able to code like
$search = Uri:segment(2);
echo '<input type="submit" name="submit" id="submit" value="'.$search.'" />';
This can be achieved several ways.
Javascript
See StarLight's answer. It's simple and does the job, but you need to check for illegal URL-characters.
PHP redirect
Example: Set form action to 'search.php'.
<?php
$urlstr = urlencode(str_replace(' ', '_', $_GET['search']));
header("Location: http://www.mysite.co.uk/find/$urlstr");
exit;
?>
To update the textbox you need the following code on the page showing the search box.
<?php
// First get $urlstr from url.
$searchstr = str_replace('_', ' ', urldecode($urlstr));
$textboxvalue = (empty($searchstr)) ? 'Search' : $searchstr;
?>
<input type="submit" name="submit" id="submit" value="<?=$textboxvalue?>" />
Thank you for reading. I'm trying to create a HTML form so that my friend can type text into it and thereafter, updates his web site with whatever is typed into the form. I'm trying to create a HTML form (on a php page) which posts whatever is entered within it's textarea to the home.php file. However, rather than simply do a "one-off" post, I'm trying to make it so that whatever is entered within the textarea saves the data into the home.php file. The home.php file is blank, and the form which I have created is as below:
<form method="post" action="home.php">
<textarea id="element" name="element" rows="15" cols="80" style="width: 80%">
</textarea>
<input type="submit" name="save" value="Save" />
<input type="reset" name="reset" value="Reset" />
</form>
For example, if the words "example" was typed into the form then submitted, the home.php file should have the words "example" written on it.
If you require more details, then please reply. Thank you. :)
<?php
$Input = $_POST['element'];
$FileToUpdate = "home.php";
$fh = fopen($FileToUpdate , 'w') or die("can't open file");
fwrite($fh, $Input);
fclose($fh);
?>
The code above will do what you wish, but will overwrite the page (to append see this reference). But really I think you need to start from basics with a good PHP Tutorial.
This should do what you want:
<?php
$filename = "/path/to/home.php";
$file = fopen( $filename, "w" );
if( $file == false ) {
echo ( "Error in opening new file" );
exit();
}
fwrite( $file, $_POST['element'] );
fclose( $file );
?>
You can read more about file I/O here.
You can use the php $_POST var to fetch the data from a form post.
For example if you want to fetch the field named "element" you can use $_POST['element']
Try the code below to display the text which was typed into the textarea. The code goes into home.php
<?php
echo $_POST['element'];
?>
Likewise you can fetch all required data. Hope this helps. Please go through http://www.w3schools.com/php/php_post.asp for more information.