首页 > 解决方案 > 在不重新加载页面的情况下运行 PHP API 调用

问题描述

我有一个多步骤表单(HTML / JS / PHP),表单数据在提交后被发送到后端,导致页面重新加载。

当用户进入表单的新页面(单击下一步按钮)时,是否可以将数据实时传递到后端?

<?php

require_once dirname(__FILE__) . '/MailWizzApi/Autoloader.php';

MailWizzApi_Autoloader::register();

// configuration object
$config = new MailWizzApi_Config(array(
    'apiUrl'        => 'http://email.mddasd.de/api/index.php',
    'publicKey'     => 'SAMPLEKEY',
    'privateKey'    => 'SAMPLEKEY',

    // components
    'components' => array(
        'cache' => array(
            'class'     => 'MailWizzApi_Cache_File',
            'filesPath' => dirname(__FILE__) . '/MailWizzApi/Cache/data/cache', // make sure it is writable by webserver
        )
    ),
));

// now inject the configuration and we are ready to make api calls
MailWizzApi_Base::setConfig($config);

// start UTC
date_default_timezone_set('UTC');
                            
$email = $_POST ['email'];
$name = $_POST['name'];
$phone = $_POST['telephone'];

if (isset($_POST['branch_0_answers']) && $_POST['branch_0_answers'] != "")
                            {
                            foreach($_POST['branch_0_answers'] as $value)
                                {
                                $art.= trim(stripslashes($value)) . "\n";
                                };
                            $grundflaeche .= $_POST['grundflaeche-grundstueck'] . "\n";
                            $plz .= $_POST['plz'] . "\n";
                            }
    
if (isset($_POST['branch_1_answers']) && $_POST['branch_1_answers'] != "")
                            {
                            foreach($_POST['branch_1_answers'] as $value)
                                {
                                $art.= trim(stripslashes($value)) . "\n";
                                };
                            $wohnflaeche .= $_POST['wohnflaeche-haus'] . "\n";
                            $grundflaeche .= $_POST['grundflaeche-haus'] . "\n";
                            $baujahr .= $_POST['baujahr'] . "\n";
                            $plz .= $_POST['plz'] . "\n";
                            }

if (isset($_POST['branch_2_answers']) && $_POST['branch_2_answers'] != "")
                            {
                            foreach($_POST['branch_2_answers'] as $value)
                                {
                                $art.= trim(stripslashes($value)) . "\n";
                                };
                            $wohnflaeche .= $_POST['wohnflaeche-wohnung'] . "\n";
                            $baujahr .= $_POST['baujahr'] . "\n";
                            $plz .= $_POST['plz'] . "\n";
                            }

if (isset($_POST['branch_3_answers']) && $_POST['branch_3_answers'] != "")
                            {
                            foreach($_POST['branch_3_answers'] as $value)
                                {
                                $art.= trim(stripslashes($value)) . "\n";
                                };
                            $gewerbeflaeche .= $_POST['gewerbeflaeche'] . "\n";
                            $grundflaeche .= $_POST['grundflaeche-gewerbe'] . "\n";
                            $baujahr .= $_POST['baujahr'] . "\n";
                            $plz .= $_POST['plz'] . "\n";
                            }

                            foreach($_POST['branch_1_1_answers'] as $value)
                            {
                            $stand.= trim(stripslashes($value)) . "\n";
                            };
        
$endpoint = new MailWizzApi_Endpoint_ListSubscribers();

$response = $endpoint->create('ma503j6m97b75', array(
    'EMAIL'    => $email,
    'TELEFON' => $phone,
    'NAME'    => $name,
    'ART' => $art,
    'GEWERBEFLAECHE' => $gewerbeflaeche,
    'WOHNFLAECHE' => $wohnflaeche,
    'GRUNDFLAECHE' => $grundflaeche,
    'BAUJAHR' => $baujahr,
    'PLZ' => $plz,
    'STAND' => $stand

));
?>  

.php 使用最终的提交按钮执行。

感谢您的回答。

标签: phpajaxformsapi

解决方案


推荐阅读