weixin_33694620 2015-01-23 17:14 采纳率: 0%
浏览 35

表单提交AJAX + PHP

On Sunday, I made a working script to send form data into my database and email with Mandrill.

Since Wednesday, my script doesn't work anymore. I'm trying to find the error but nothing fixed it.

I think I have an error in my PHP script but I don't know where and what. Console log of my AJAX returns correct data but PHP doesn't do anything. It doesn't receive data.

There is my HTML

<form method="POST" id="formContact" action"#">
                <input type="text" placeholder="Prénom" name="name" id="formName">
                <input type="email" placeholder="Email" name="email" id="formEmail">
                <input type="text" placeholder="Sujet" name="subject" id="formSubject">
                <textarea cols="30" rows="10" placeholder="Ton message" name="message" id="formMessage"></textarea>
                <button type="submit" id="formSubmit">Envoyer</button>
            </form>

JavaScript

$(document).ready(function() {
$('#formContact').submit(function(event) {
    var formData = {
        'name' : $('#formName').val(),
        'email' : $("#formEmail").val(),
        'subject' : $("#formSubject").val(),
        'message' : $("#formMessage").val()
    };
    $.ajax({
        url: "commons/test.php",
        type: "POST",
        data: formData,
        dataType: 'json',
        encode: true
    })
    .done(function(data){
        console.log(formData);
        console.log(data);

        if( ! data.success){
            alert('Error');
        }else{
            alert('Success');
        }
    });
    event.preventDefault();
  });
});

PHP

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
include('../adm/dbconnect.php'); <- this work well
    $data = array();
    if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['subject']) && !empty($_POST['message']))
    {
        // var_dump($_POST['name']);
        // var_dump($_POST['email']);
        // var_dump($_POST['subject']);
        // var_dump($_POST['message']);

        $to = 'contact@sheguey.land';
        $content = ''.$_POST['message'].'';
        $subject = 'Nouveau message de '.$_POST['email'].' - Sheguey Land';
        $from = ''.$_POST['email'].'';

        $uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
        $api_key = 'my mandrill api key';
        $content_text = strip_tags($content);

        $postString = '{
            "key": "' . $api_key . '",
            "message": {
            "html": "' . $content . '",
            "text": "' . $content_text . '",
            "subject": "' . $subject . '",
            "from_email": "' . $from . '",
            "from_name": "' . $from . '",
            "to": [
            {
                "email": "' . $to . '",
                "name": "' . $to . '"
            }
            ],
            "track_opens": true,
            "track_clicks": true,
            "auto_text": true,
            "url_strip_qs": true,
            "preserve_recipients": true
            },
            "async": false
        }';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
        $result = curl_exec($ch);



        $req = $bdd->prepare('INSERT INTO contactform (name, email, subject, message) VALUES( :name,  :email,  :subject,  :message)');
        $req->execute(array(
                'name' => $_POST['name'],
                'email' => $_POST['email'],
                'subject' => $_POST['subject'],
                'message' => $_POST['message']
            ));
        $data['success'] = true;

    }else{
        $data['success'] = false;
    }
    echo json_encode($data);

After two days of research (changing PHP script, AJAX script, etc), I don't understand why this don't work anymore.

  • 写回答

3条回答 默认 最新

  • weixin_33744854 2015-01-23 18:07
    关注

    you might need quotes in formData '"' + $('#formName').val() + '"'

    评论

报告相同问题?

悬赏问题

  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错