I came up with a app idea that allows Link Searches for a website. I am trying to get the app to automatically save searches for the user.
Here is some of my code that is written up. What I am trying to achieve is the ability to enter random links from websites that I like and display the links within the app and automatically save the inputs.
Is there a way to save links within a page in PHP? Fairly new to PHP. Sorry!
Here is a link - http://www.andulicsdesign.com/Blakes/Index.php
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Indeed Link Search App</title>
<script type="text/javascript">
$(document).ready(function() {
$('#formid form').submit(function(){
$.get('result.php', $(this).serialize(), function(data){
$('#result').html(data);
});
return false;
});
});
</script>
</head>
<body>
<div id="container>
<div id="formid">
<form>
Job Title<input type="text" name="message" value="" />
<input type="submit" value="Submit" />
</form>
</div>
<div id="result">
<?php
echo '<div style="background-color:#fff; padding:20px">' . $_POST['message'] . '</div>';
?>
<?php
$message=$_REQUEST['message'];
echo $message;
?>
</div>
</div>
</body>
</html>
There are some options for saving information.
The simplest is save in the session, but the list will be cleared when you close your browser.
session_start(); //in the begin of file
$_SESSION['links'][] = $_REQUEST['message']; //add to array of links
$_SESSION['links'] = array_unique($_SESSION['links']); //removes duplicate links
foreach($_SESSION['links'] as $link) {
// do something with each link in array
}
To keep the information between sessions you need to use a database like MySQL.
I interpreted your question as a one regarding taking user form data, processing it using PHP, and then returning that data to be displayed. Let me know if I'm wrong.
You should be able to do that using an asynchronous JavaScript request on the page you intend to display the results on (which you've pretty much got). Not sure what that PHP code is for at the bottom, but it isn't necessary to display the results from result.php
.
$(document).ready(function() {
$('#formid form').submit(function(e){
e.preventDefault();
$.get('result.php', $(this).serialize(), function(data){
$('#result').append(data); // If you're looking to save links and not write over previously searched results, then use append.
});
});
});
The above code is assuming that your result.php
produces straight HTML that can be immediately rendered. If that isn't the case, obviously you'll need to parse the results using JavaScript (JSON is pretty useful for this). Please note that this will only save the results the current user is searching for — as tttony pointed out, use a database to permanently store data.
Also, you have some syntax errors: <div id="container>
should be <div id="container">
. Remember to include the jQuery library in your <head>
as well.