如何将动作和发布方法一起使用?

问题描述 投票:0回答:2

下面是我的代码。如果我不以action =“ https://www.paypal.com/cgi-bin/webscr”形式使用此链接。数据形式成功插入到我的数据库中。但是,当我使用此操作链接时。在数据库中,表单数据未发送

所以,表单动作和发布方法如何一起工作?

实际上这是我想要的,单击提交按钮后,将标题数据成功插入到我的数据库中。然后页面重定向到PayPal结帐页面。

<?php
$database_username = 'root';
$database_password = '';
$pdo_conn = new PDO( 'mysql:host=localhost;dbname=shipping_pro', $database_username, $database_password );

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['insert'])) {
  $sql = "INSERT INTO posts (title) VALUES (:title)";
  $pdo_statement = $pdo_conn->prepare( $sql );
  $result = $pdo_statement->execute( array( 
    ':title'=>$_POST['title']
  ) );
  if (!empty($result) ){
    header('location:index.php');
  }
}
?>
<form method="POST" action="https://www.paypal.com/cgi-bin/webscr">
  <input type="text" name="title" placeholder="Title" />
  <input name="insert" type="submit" value="Add">
</form>
php mysql database forms action
2个回答
0
投票

[我刚刚快速测试了我在上面的评论中提到的use ajax and let the callback submit the form想法-似乎可以正常运行,因此很想知道为什么这对您不起作用-本质上,该表格对于Paypal而言在所有意图和目的上都将出现应当这样,所以我看不到为什么它不起作用(但可能还没有足够的咖啡吗?)

通过防止单击(e.preventDefault())时SUBMIT按钮执行其默认操作,您可以中断表单的流程并可以在提交之前注入自己的逻辑。而不是立即发送表单,而是将所需的任何数据发送到PHP脚本,而是将其记录到db并发送给ajax函数一个答复,说“嗨,我已经完成-继续”等,然后回调实际上将继续提交表格。

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        ob_clean();

        /* log details to db */

        /*

        $database_username = 'root';
        $database_password = '';
        $pdo_conn = new PDO( 'mysql:host=localhost;dbname=shipping_pro', $database_username, $database_password );

        INSERT INTO posts (title) VALUES (:title) etc etc

        */

        /* send some sort of response to ajax callback */
        exit("banana's tend to be yellow and monkeys love 'em!");
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>Paypal: Record details to db and then submit to Paypal</title>
        <script>
            const ajax=function(url,params,callback){
                let xhr=new XMLHttpRequest();
                xhr.onload=function(){
                    if( this.status==200 && this.readyState==4 )callback.call( this, this.response )
                };
                xhr.onerror=function(e){
                    alert(e)
                };
                xhr.open( 'POST', url, true );
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xhr.setRequestHeader('X-Requested-With','XMLHttpRequest');
                xhr.send( params );
            };

            const clickhandler=function(e){
                e.preventDefault();

                let form=this.parentNode;

                const callback=function(r){
                    alert( 'Response from saving to db: '+r+'\nNow submit the form to PayPal!!!' );
                    form.submit();
                };
                ajax( location.href, 'title='+this.previousElementSibling.value, callback )
            };
            document.addEventListener('DOMContentLoaded', ()=>{
                document.querySelector( 'form > input[type="submit"][name="insert"]' ).addEventListener( 'click', clickhandler )
            });
        </script>
    </head>
    <body>
        <form method="POST" action="https://www.paypal.com/cgi-bin/webscr">
            <input type='hidden' name='cmd' value='_s-xclick' />
            <input type='hidden' name='hosted_button_id' value='S2QXVVPPL7EUE' />
            <input type='hidden' name='on0' value='BANANA' />
            <input type='hidden' name='os0' value='BANANA' />
            <input type='hidden' name='currency_code' value='GBP' />

            <input type="text" name="title" placeholder="Title" />
            <input name="insert" type="submit" value="Add">
        </form>
    </body>
</html>

0
投票

请尝试以下代码

<?php
$database_username = 'root';
$database_password = '';
$pdo_conn = new PDO( 'mysql:host=localhost;dbname=shipping_pro', $database_username, $database_password );

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['insert'])) {
  $sql = "INSERT INTO posts (title) VALUES (:title)";
  $pdo_statement = $pdo_conn->prepare( $sql );
  $result = $pdo_statement->execute( array( 
    ':title'=>$_POST['title']
  ) );
  if (!empty($result) ){
    header('location:https://www.paypal.com/cgi-bin/webscr')
  }
}
?>
<form method="POST" action="index.php">
  <input type="text" name="title" placeholder="Title" />
  <input name="insert" type="submit" value="Add">
</form>
© www.soinside.com 2019 - 2024. All rights reserved.