dqst96444 2017-07-27 22:48
浏览 38

在没有文本输入的表单提交上执行PHP代码

I just want to know if it is possible to execute a PHP post request with ajax by click on a form submit button that does not have any text input. Say i have a form

<form action="connecting.php" id="connect" method="post" enctype="multipart/form-data">
  <input type="submit" name="userconnect" id="userconnect" value="connect">
</form>

And i want to execute this 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) as count FROM connect WHERE (user_one = '$my_id' AND user_two = '$user_id') OR (user_two = '$my_id' AND user_one = '$user_id');
EOF;

        $hret = $db->querySingle($hsql);

        if ($hret == 1)
        {
            echo "<script>alert('You are already connected to this user')</script>";
        }
        else
        {

            $usql = $db->prepare("INSERT INTO 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 is the ajax request i am trying to use

$("#connect").submit(function(e) {
e.preventDefault();
$.ajax({
    type: "POST",
    url: "connecting.php",
    data: $(this).serializeArray(), //the data is an issue cause of no text input in for 
    dataType: "json", 
    success: function(response) {
        console.log(response);
    },
    error: function(response) {
    }
});
});

This doesn't work because there is no data in the form for ajax to send but all data are in the PHP file. How do i execute the php file with ajax. Is there any way?

  • 写回答

2条回答 默认 最新

  • dpa0760 2017-07-27 22:51
    关注

    You dont have to send a POST request to a PHP script in order for it to execute. You can send a normal GET to a URL that executes a PHP script and send you back the JSON data you are looking for. That way, you dont have to send data with the post request and you can still run your PHP script.

    Edit* Another thing you can do is add a hidden ID field or some random text and send that with the POST as data. You dont have to do anything with it, it could just be a time stamp, but it will send the POST request. This is of course overhead and not my kind of programming, but an option nonetheless.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题