简体   繁体   中英

passing data from javascript using ajax and saving it to a database

Hi guys I currently have a website that lets users search a website, I would like to save these search terms and the amount of times each has been searched in a database. To this end I have created a table with 2 columns search which is varchar(30) and value which is int(30) search is set to unique so that terms for instance design don't populate two rows however when i run the program nothing is being saved to the database i was wondering if any of you could help.

javascript

function performSearch() {
    var searchURL = "http://api.lmiforall.org.uk/api/v1/soc/search?q=";
    var searchTerms = $('#searchterm').val();
    var searchReady = searchTerms.toLowerCase();
    $.ajax({
                    type: "POST",
                    url: 'findfavourites.php',
                    data: {searchReady : searchReady},
                    success: function(data)
                    {
                        alert("success!");
                    }
                });
    $('#soctable tbody').empty();
    $.getJSON(searchURL + searchReady, function(results) {
        results.forEach(function (result) {
        var row = $("<tr></tr>");
        var codeCell = $("<td></td>");
        var titleCell = $("<td></td>");
        var descriptionCell = $("<td></td>");
        var qualificationsCell = $("<td></td>");
        var tasksCell = $("<td></td>");
        codeCell.html(result.soc);
        titleCell.html(result.title);
        descriptionCell.html(result.description);
        qualificationsCell.html(result.qualifications);
        tasksCell.html(result.tasks);
        row.append(codeCell);
        row.append(titleCell);
        row.append(descriptionCell);
        row.append(qualificationsCell);
        row.append(tasksCell);
        $('#soctable tbody').append(row);
    });
   });

}


$(function() {
    // when the page is loaded
    $('#dosearch').on('click', performSearch);
});

PHP

<?php

    include 'includes/dbConnection.php';
    if(isset($_POST['searchReady']))
    {
          $uid = $_POST['searchReady'];
      $value='1';
    $query=$conn->prepare("INSERT INTO favourites VALUES        ('$uid','$value') ON DUPLICATE KEY UPDATE value = VALUES(value + 1");
    $query->execute();
    $conn = null;
}   
?>

any help would be appreciated thanks in advance.

edit

@PHPglue

im sure you were asking for the page the getjson returns results to if so here is the html page

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, intial-scale=1">
<title>Homepage</title>
<link href="styleSheet.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="jquery-3.1.1.js"></script>
<script type="text/javascript" src="soc.js"></script>
</head>

<body>
<div id="page">
<nav>
  <ul>
    <li><a href="index.html">Home Page</a></li>
    <li><a href="careers.html">Careers</a>
    <ul>
    <li><a href="#">Careers and Apprenticeships</a></li>
    </ul>
    </li>
  </ul>
</nav>
<header>
  <h1><u>Careers and Apprenticeships</u></h1>
</header>
<div class="container">
<div class="row">
<div class="col-sm-12">
<input class="form control" type="text" id="searchterm" placeholder="Put your search term here...">
<button class="btn btn-primary" id="dosearch">Search!</button>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-sm-12">
<table class="table table-striped" id="soctable">
<thead>
<tr>
<th>SOC Code</th>
<th>Job Title</th>
<th>Description</th>
<th>Qualifications</th>
<th>Tasks</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
<footer>
</footer>
</body>
</html>

EDIT 2

Hi everyone I have discovered that the ajax is working and the problem is cause by the ON DUPLICATE KEY UPDATE could any one perhaps check the syntax and tell me if you see any problems. As I said above I would like this value to update to 2 when the same term is searched and so on.

Try amending the sql line to:

$query=$conn->prepare("INSERT INTO favourites 
  VALUES ('$uid','$value') 
  ON DUPLICATE KEY UPDATE value = value + 1");

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM