Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery AJAX with PHP to upload contents to MYSQL DB
    text
    copied!<p>I am looking for a jQuery AJAX script alongside a PHP script that allows for the storage of information on a button click. The function defined within the jQuery should take three variables, all of which are defined pre-method call. I have the basis of operation complete but at the end of all operations - after the button is clicked and some time has passed - no data is added to the appropriate mysql database.</p> <p>Here is my jQuery function "store"</p> <pre><code>&lt;script type="text/javascript"&gt; function store(ud, ld, tp) { $.ajax({ url: 'http://www.exampledomain.com/folder/store.php', type: 'POST', data: 'ud='+ud+'&amp;ld='+ld+'&amp;tp='+tp success : function() { alert("WORKED!"); }, error : function() { alert("DIDN'T WORK!"); }, complete : function() { } }); } &lt;/script&gt; </code></pre> <p>Here is the store.php file (very basic I know, I have also yet to secure this script via sanitizing user input)</p> <pre><code>&lt;?php require ('../mysqli_connect.php'); $errors = 0; if(isset($_POST['ud']) &amp;&amp; is_numeric($_POST['ud'])) { $ud = $_POST['ud']; } else { ++$errors; } if(isset($_POST['ld']) &amp;&amp; is_numeric($_POST['ld'])) { $ld = $_POST['ld']; } else { ++$errors; } if(isset($_POST['tp'])) { $tp = strip_tags(stripslashes($_POST['tp'])); } else { ++$errors; } if($errors == 0) { $q = "INSERT INTO table_name (column_1, column_2, column_3, column_4) VALUES ('$ld', '$ud', NOW(), '$tp')"; mysqli_query($mysqli, $q); } else { echo 'There was a problem!'; } ?&gt; </code></pre> <p>Assume that I have onclick="store(3, 3, A)" as an attribute for a certain element. How can I fix this? If I remove the onclick attribute how do I pass the necessary parameters to the jQuery function? I appreciate any and all help!</p> <p>&lt;-- EDIT --></p> <p>New <strong>jQuery &amp; AJAX Script</strong> ...</p> <pre><code>&lt;script type="text/javascript"&gt; function store(ud, ld, tp) { $.ajax({ url: 'http://www.exampledomain.com/folder/store.php', type: 'POST', data: 'ud='+ud+'&amp;ld='+ld+'&amp;tp='+tp, error : function() { alert("error"); }, success : function(data) { alert(data); }, complete : function() { alert("complete"); } }); } $(function () { $("a.rec").on("click", function () { var $this = $(this), ud = $this.data("ud"), ld = $this.data("ld"), tp = $this.data("tp"); store(ud, ld, tp); }); }); &lt;/script&gt; </code></pre> <p>Revised <strong>PHP</strong></p> <pre><code>&lt;?php if($_SERVER['REQUEST_METHOD'] === "POST"){ require ('../mysqli_connect.php'); $errors = 0; if(isset($_POST['ud'])) { $ud = $_POST['ud']; } else { ++$errors; } if(isset($_POST['ld'])) { $ld = $_POST['ld']; } else { ++$errors; } if(isset($_POST['tp'])) { $tp = $_POST['tp']; } else { ++$errors; } if($errors == 0) { $q = "INSERT INTO table_name (column_1, column_2, column_3, column_4) VALUES ('$ld', '$ud', NOW(), '$tp')"; mysqli_query($mysqli, $q); } else { echo 'There was a problem!'; } } else { $url = 'http://www.exampledomain.com/error.php'; ob_end_clean(); header("Location: $url"); exit(); } ?&gt; </code></pre> <p>Now for my <strong>HTML</strong></p> <pre><code>&lt;li&gt; &lt;div class="sample classes"&gt; &lt;a class="rec" data-ud="13" data-ld="10" data-tp="SCI"&gt; &lt;input type="submit" title="Something" value="Something" /&gt; &lt;/a&gt; &lt;/div&gt; &lt;/li&gt; </code></pre> <p>However, when this button is clicked, it still does not do anything!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload