0

so I have a div called #preview which is just a preview of a survey. I want to send the html value of the div and send it to a GET variable valled content so i can use it in php. here's what i've done:

$('#create-button').click(function(event){
            event.preventDefault();
            $.ajax({
                url:'../php/CheckForExistingSurveys.php',
                data:{content: $('#preview').html()}
            });
        });

in my php script:

<?php 
    if(isset($_GET['content'])){
        echo 'content is set';
    }
?>

whenever i click the #create-button i dont see the get variable being initialized in the url. All I want to know is how i can get the html value of the #preview div and send it to a get variable so that I can make queries and what not in the CheckForExistingSurveys.php file.

Angel Garcia
  • 1,547
  • 1
  • 16
  • 37
  • 1
    That's what `event.preventDefault();` is supposed to do. Prevent the default action. So you don't see any changes in your URL. Instead, the data is being send with Ajax. Perhaps you should read more about that first. This would be a good place to start: [jQuery Ajax POST example with PHP](https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php). Once you understand how Ajax works, you'll understand what you're missing in your code and how to fix it. Good luck! – icecub Aug 07 '17 at 04:22
  • @icecub Thank you i definitely will! – Angel Garcia Aug 07 '17 at 04:26
  • something like :- `url:'../php/CheckForExistingSurveys.php'+$('#preview').html()`, but untill we can't see what you have in your `preview` div we can't give you 100%correct idea – Alive to die - Anant Aug 07 '17 at 04:26

1 Answers1

-1

HTML:

<form action='' method='' id="myform">
    Name:<input type='text' name='name' id='name'>
    <input type='submit' value='Send'>
</form>

Javascript:

$('#myform').submit(function(){

    var name = $('#name').val();

    $.ajax({
        type: "POST",
        url: "your_page_with_php_script.php",
        data: "name=" + name,
    })
    .done(function( msg ) {
        alert( "Data Saved: " + msg );
    });
});

PHP:

<?php

if (isset($_POST['name'])) {

    $a = $_POST['name'];

    if ($a != NULL) {
        echo "Success " . a;
    }
};

?>
Anh Pham
  • 2,108
  • 9
  • 18
  • 29