1

I need to do the following:

  1. Catch a URL pasted into a simple HTML text box on the paste event and save to a JavaScript variable called myURL (this code works)

  2. Send the myURL variable using AJAX to a PHP page that will scrape some content from the URL. The PHP page (webscraper.php) will save the scraped content in the database and then also display the scraped content on the HTML page (where the text box is) without reloading the page. And this step is where the code is missing and broken.

index.html:

<body>
     <input type="text" class="newslinkinput"/>
</body>

URLonpaste.js:

$(document).ready(function () {
 $(".newslinkinput").bind('paste', function (e) {
     setTimeout(function () {
         var myURL = $(".newslinkinput").val()
         $.ajax({
             type: "POST",
             url: "webscraper.php",
             data: "newslink=" + myURL.val(),
             success: function (data) {}
         });
     }, 0);
   });
 });

webscraper.php:

<?php   
$newslink = $_POST['newslink'];

require_once('ExternalScraper.php');

$result = ExternalScraper::fetch($newslink);

$contentA = $result->contentA;
$contentB = $result->contentB;

include "include/database.php";

$insert = mysqli_query($connect, "INSERT INTO mytable (contentA, contentB) VALUES ('$contentA', '$contentB')");

mysqli_close($connect);

//Somehow get $contentA and $contentB to display on index.html
//Do all this without refreshing the page

?> 
4
  • I don't know if ExternalScraper makes things safe, but to avoid doubt, you should parameterise your INSERT query, to be doubly sure you don't have a SQL injection vulnerability. Commented Apr 25, 2014 at 18:37
  • What is the purpose of the setTimeout in your event handler? Commented Apr 25, 2014 at 18:39
  • @halfer: onpaste can fire before the input's .value changes, setTimeout gives the new text time to "sink in" Commented Apr 25, 2014 at 19:16
  • @dan, thought so, but I'd have expected jQuery to do that :). But no matter. OP, to debug this, look at a live AJAX viewer in your browser. Do you get a successful (HTTP 200 code) response? Do you get an error in the response? Commented Apr 25, 2014 at 19:37

1 Answer 1

1

Try this:

index.html:

<body>
     <input type="text" class="newslinkinput"/>
     <div id="contentA"></div>
     <div id="contentB"></div>
</body>

URLonpaste.js:

...
$.ajax({
    type: "POST",
    url: "webscraper.php",
    data: "newslink=" + myURL.val(),
    dataType: "json",
    success: function (data) {
        $('#contentA').html(data.contentA);
        $('#contentB').html(data.contentB);
    }
});
...

webscraper.php (append to the end):

...
echo json_encode(array('contentA' => $contentA, 'contentB' => $contentB));
Sign up to request clarification or add additional context in comments.

2 Comments

@halfer Thanks for the note. Fixed
Works great, thanks! The only change I had to make was in the AJAX, changing myURL.val() to just myURL, then it worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.