-1

So im learning about jquery and ajax. Originally i did an old school php page, where when i submit the form, the data gets stored in a database, and then the webpage loads a new page and tells me i've successfully added a student... I'm not trying to implement ajax and jquery to my code, but i my js script isn't working. I'm trying to call an Alert() just to test if jquery is working, but no alert popup appears when i click the submit button.

This is what i have so far:

addStudent2.php code:

<!DOCTYPE html>
<html>
<head>
    <title>Adding Student With AJAX</title>
</head>
<body>

<form action = "userInfo.php" id="myForm" method="post">
<p>Name: 
<input type="text" name="name" value=""/>
</p>

<p>Age: 
<input type="text" name="age" value=""/>
</p>

<input type="submit" id="submit" value="Add"/>

<div id="result"></div>

<script src="http://code.jquery.com/jquery-3.2.0.min.js" type = "text/javascript"></script>
<script src="my_script.js" type = "text/javascript"></script>
</form>
</body>
</html>

userInfo.php code:

<?php
    include('connection.php');

    $name = $_POST['name'];
    $age  = $_POST['age'];
    $query = "INSERT INTO student2 (first_name, age) VALUES(?, ?)";
    $var = array($name,$age);
    $response = sqlsrv_query($conn, $query, $var);


    if($response == true){
        echo "Student has been added";
    }
    else{
        echo nl2br("Insertion failed\n");
        die( print_r( sqlsrv_errors(), true));
    }
?>

my_script.js code:

$('input#submit').on('click', function(){
    alart(1);
});

i know my javascript or jquery library isn't working, because i'm not getting an alert popup when i click on the submit button.

Could someone please help?

Barcode
  • 930
  • 1
  • 13
  • 31
  • 2
    because you have `alart` instead of `alert`? – Cfreak Apr 04 '17 at 20:14
  • you should do practice to check your browser console when you are working with jquery or javascript in future – Mahesh Singh Chouhan Apr 04 '17 at 20:17
  • Someone should write a polyfill for alart() that pops up the annoying alert modal and plays an alarm sound. – Matt Apr 04 '17 at 20:20
  • And one more thing try to prevent default submit action for the form or just dont use input type as submit if you want that form to be submitted using JS (AJAX). – BetaDev Apr 04 '17 at 20:21
  • looool.... but yeah it still doesnt work :/ It's like my js scrip isnt even being read at all. – Barcode Apr 04 '17 at 20:22
  • hey use input `type=button` not submit and just `$('#submit')` at JQuery Side – BetaDev Apr 04 '17 at 20:23
  • First.. Open up web developer tools in your browser... Look under source tab, and see if its located there.. Or even in the console log. type $ or jQuery.. If neither of those produces sufficient evidence you probably need to read how to use javascript, or link scripts. start here. https://www.w3schools.com/html/ – Cam Apr 04 '17 at 20:27
  • You ay want to look into this project: https://github.com/ajax-proofs/proofs – a coder Apr 04 '17 at 20:29
  • Never use `id="submit"`. it overrides `form.submit`. probably better to bind to the form's submit event anyway. – Kevin B Apr 04 '17 at 20:34
  • @Cam It is located there .Under Localhost are "addStudent2.php" and "my_script.js" – Barcode Apr 04 '17 at 20:36
  • Of course.. But just because you wrote it, doesnt mean that its correct.. A bare minimum amount of error checking can find the issue quickly. – Cam Apr 04 '17 at 20:41
  • @Cam i'm just not sure where to go from here. I've change the input type to "button", but i still have the same issue. I just started php,jquery and all of this a few days ago, sorry if im very slow. – Barcode Apr 04 '17 at 20:44
  • Ok... Simple test.. put this in your script. alert('test'); – Cam Apr 04 '17 at 20:45
  • @Cam `$('#submit').on('click', function(){ alert('test'); });` Doesnt' work. (on jquery side) – Barcode Apr 04 '17 at 20:47
  • Notice. I didnt say to do on click. In Javascript you can run an alert just on its loading. – Cam Apr 04 '17 at 20:52
  • also... change your on click to function(e) { alert('test'); e.preventDefault(); } – Cam Apr 04 '17 at 20:53

1 Answers1

0

The problem was the js script was not updating on the browser, i had to force refresh the page by pressing "Ctrl + F5" here.

Also I changed input#submit to #submit:

$('#submit').on('click', function(){
    alert(1);
});

...and of course changed "alart(1)" to "alert(1)" lol.

Community
  • 1
  • 1
Barcode
  • 930
  • 1
  • 13
  • 31