Skip to content
Advertisement

i want to send a javascript prompt answer to my sql databse, is this possible or is there a better way to do this? [closed]

currently have this code activated by a html button:

function popup() {
    var group = prompt("Please enter group name");
    if (group != null || group != "") {
        window.location.href = "template.php?groupid=" + group; //groupid
    }
}

i wanna send the value of group in a sql query in some way, so it can creaty a unique id and use that as my groupid. i think i have to use ajax, but im unable to find a tutorial for this.

Advertisement

Answer

  1. Add jquery.js reference (e.g. jquery-1.10.2.js)
  2. use ajax in place of your original call to template.php
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>

function popup() {
    var group = prompt("Please enter group name");
    if (group != null || group != "") {

//        window.location.href = "template.php?groupid=" + group; //groupid


 $.ajax({
    type: "POST",
    dataType: 'text',
    url: "template.php?groupid" + group,
    success: function(response){
        //if request if made successfully then the response represent the data
// do something after the ajax is done. e.g. alert(response)
//the response can be your groupid which the template.php echo execution

    }
  });

  }
}

</script>

For example in your template.php:

<?php
// include "db.php";
/// process the groupid=group
//// further processing to generate the unique $groupid

echo $groupid;
?>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement