javascript - Execute PHP code on form submit without text input -
i want know if possible execute php post request ajax click on form submit button not have text input. have form
<form action="connecting.php" id="connect" method="post" enctype="multipart/form-data"> <input type="submit" name="userconnect" id="userconnect" value="connect"> </form>
and want execute block of php code on submit button
connecting.php
<?php require_once ("db.php"); $db = new mydb(); session_start(); if (isset($_post['userconnect'])) { $my_id = htmlspecialchars($_session['log_id'], ent_quotes, 'utf-8'); $user_id = (int) htmlspecialchars($_session['userid'], ent_quotes, 'utf-8'); $rand_num = rand(); $hsql =<<<eof select count(hash) count connect (user_one = '$my_id' , user_two = '$user_id') or (user_two = '$my_id' , user_one = '$user_id'); eof; $hret = $db->querysingle($hsql); if ($hret == 1) { echo "<script>alert('you connected user')</script>"; } else { $usql = $db->prepare("insert connect (user_one, user_two, hash) values (:my_id, :user_id, :rand_num)"); $usql->bindvalue(':my_id', $my_id, sqlite3_integer); $usql->bindvalue(':user_id', $user_id, sqlite3_integer); $usql->bindvalue(':rand_num', $rand_num, sqlite3_text); $uret = $usql->execute(); if (!$uret) { echo "error connecting"; } else { echo "connection sucessful"; } } }
this ajax request trying use
$("#connect").submit(function(e) { e.preventdefault(); $.ajax({ type: "post", url: "connecting.php", data: $(this).serializearray(), //the data issue cause of no text input in datatype: "json", success: function(response) { console.log(response); }, error: function(response) { } }); });
this doesn't work because there no data in form ajax send data in php file. how execute php file ajax. there way?
you dont have send post request php script in order execute. can send normal url executes php script , send json data looking for. way, dont have send data post request , can still run php script.
edit* thing can add hidden id field or random text , send post data. dont have it, time stamp, send post request. of course overhead , not kind of programming, option nonetheless.
Comments
Post a Comment