RajkumarLA Posted May 26, 2017 Share Posted May 26, 2017 Hi, i am new to ajax. please help on this simple form submit. i have simple form, after submit the form want to display 'successfully submitted' message in the same page.. my form <form align="left" method="post" class="subscribe_form" action='subscribe.php'> Your Name:<br> <input type="text" name="name" value="" required><br> Your E-Mail:<br> <input type="email" name="email" value="" required><br><br> Gender: <p> <input name="gender" value="male" type="radio" id="male" /> <label for="male">Male</label> <input name="gender" value="female" type="radio" id="female" /> <label for="female">Female</label> </p> <br> Company Name: <input type="text" name="cname" value="" required><br><br> <input type="submit" name="send" value="Subscribe" id="subscribe"> <span class="output_result"></span> </form> ajax code : <script> $(document).ready(function() { $('.subscribe_form').on('submit',function(){ // Add text 'loading...' right after clicking on the submit button. $('.output_result').text('Sending...'); var form = $(this); $.ajax({ type:'post', url:'subscribe.php', dataType: "text", data: form.serialize(), success: function(result){ if (result == 'success'){ $('.output_result').text('successfully submitted!'); } else { $('.output_result').text('Error!'); } } }); // Prevents default submission of the form after clicking on the submit button. return false; }); }); </script> subscribe.php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "rm"; $name = $_POST['name']; $gender = $_POST['gender']; $email = $_POST['email']; $cname = $_POST['cname']; $sub_date = date("Y-m-d"); // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "insert into rmsubscribe (name, gender, email, cname, sub_date) values ('$name', '$gender','$email','$cname','$sub_date')"; $result = (mysqli_query($conn, $sql)); echo ($result) ? 'success' : 'error'; mysqli_close($conn); this code gives me ' Error' message after submit. if i skip the ajax removing the 'class="subscribe_form" from the form and in subscribe.php using this code if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } instead of this code $result = (mysqli_query($conn, $sql)); echo ($result) ? 'success' : 'error'; works fine.. 'New record created successfully' and data inserted into table. Please help me on ajax code. i am not familiar with ajax. how to make this work using ajax? all i want is, after the data inserted into table, display a 'successful submitted' message in the form. Quote Link to comment https://forums.phpfreaks.com/topic/304014-php-simple-form-usbmit-using-ajax/ Share on other sites More sharing options...
Jacques1 Posted May 26, 2017 Share Posted May 26, 2017 Your code has plenty issues, and the complete lack of error handling which makes it impossible to figure out what actually happened is one of them. First off: Do -- not -- insert -- raw -- PHP -- variables -- into -- query -- strings. If you don't understand why, see Bobby Tables. Don't even insert your own input, because this can easily lead to a syntax errors and crash the query. Instead, use prepared statements where the input is safely passed to parameters. Unfortunately, mysqli is a very complicated database interface, and since you obviously haven't bothered to learn it, I suggest you switch to PDO instead. This will make things a lot easier. Secondly: Validate the input. Don't just assume that it's right. Check for missing fields, empty fields, wrong values. Catch problems as early as possible instead of letting them crash your application. And then you need proper error handling Enable exceptions in the database interface, so that failed queries automatically trigger an error. PHP will log this error (if your configuration is right) and tell you exactly what the problem is. Don't invent your own error responses. HTTP has status codes for that. Uncaught PHP exceptions automatically trigger a 500 code, and you can set the code yourself with http_response_code(). For example, invalid input should trigger a 400 code. Then send a JSON response with a user-friendly error message (not a PHP error message). Quote Link to comment https://forums.phpfreaks.com/topic/304014-php-simple-form-usbmit-using-ajax/#findComment-1546828 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.