さて、私はしばらくこのコードに取り組んできました。SecurImages のプリセット コードから始めて、キャプチャを検証してフォームを送信するように微調整しました。問題は、私たちのフォームがそれを作成し、データを独自のデータベースに送信するサードパーティのサイトに送信され、私は彼らのコードにアクセスできないことです。いくつかの非表示フィールドを使用して情報を取得します。たとえば、一般的な Web サイトにリンクし、ページがフォームから送信されたことを確認し、送信の詳細を確認して、どのフォームとどこに情報を送信するかを決定します。私の問題は、キャプチャを検証してフォームを送信しないか、フォームを送信してキャプチャを検証しないかのいずれかです。両方できないみたい。現在、キャプチャは正しく検証できますが、フォームが正しく送信されません。
function processForm()
{
new Ajax.Request('<?php echo $_SERVER['PHP_SELF'] ?>', {
method: 'post',
parameters: $('zoho1').serialize(),
onSuccess: function(transport) {
try {
var r = transport.responseText.evalJSON();
if (r.error == 0) {
alert ("You are the man!");
} else {
alert("There was an error with your submission.\n\n" + r.message);
}
} catch(ex) {
alert("There was an error parsing the json");
}
},
onFailure: function(err) {
alert("Ajax request failed");
}
});
return false;
}
ユーザーが送信ボタンをクリックすると、この AJAX クエリが実行されます。
function process_si_zoho1()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do'] == 'contact') {
// if the form has been submitted
foreach($_POST as $key => $value) {
if (!is_array($key)) {
// sanitize the input data
if ($key != 'LEADCF3') $value = strip_tags($value);
$_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
}
}
/*
$name = @$_POST['First Name'] . @$_POST['Last Name']; // name from the form
$email = @$_POST['Email']; // email from the form
$mainphone = @$_POST['Phone']; // url from the form
$mobile = @$_POST['Mobile']; // the message from the form
$state = @$_POST['State']; // the state from the form
$type = @$_POST['LEADCF4']; // Type of student from form
$enrollment = @$_POST['LEADCF2']; //Expected enrollment
$time = @$_POST['LEADCF5']; //Intended enrollment status
$degree = @$_POST['LEADCF6']; //Intended degree
$message = @$_POST['LEADCF3']; //How did you hear about TTU?
$comments = @$_POST['Description']; //Comments*/
$captcha = $_POST['captcha_code']; // the user's entry for the captcha code
//$name = substr($name, 0, 64); // limit name to 64 characters
$errors = array(); // initialize empty error array
if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
// only check for errors if the form is not in debug mode
if (strlen($email) == 0) {
// no email address given
$errors['email_error'] = 'Email address is required';
} else if ( !preg_match('/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $email)) {
// invalid email format
$errors['email_error'] = 'Email address entered is invalid';
}
if (strlen($message) < 10) {
// message length too short
$errors['message_error'] = 'Please enter a message';
}
}
// Only try to validate the captcha if the form has no errors
// This is especially important for ajax calls
if (sizeof($errors) == 0) {
require_once dirname(__FILE__) . '/securimage.php';
$securimage = new Securimage();
if ($securimage->check($captcha) == false) {
$errors['captcha_error'] = 'Incorrect security code entered';
}
}
if (sizeof($errors) == 0) {
// no errors, send the form
// header('http://crm.zoho.com/crm/WebToLeadForm');
/*if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
//send the message with mail()
header('location:http://www.tntemple.edu/request-information-online-learning-thank-you');
}*/
$return = array('error' => 0, 'message' => 'OK');
die(json_encode($return));
} else {
$errmsg = $captcha_error;
foreach($errors as $key => $error) {
// set up error messages to display with each field
$errmsg .= " - {$error}\n";
}
$return = array('error' => 1, 'message' => $errmsg);
die(json_encode($return));
}
} // POST
} // function process_si_zoho1()
この関数は、ページが読み込まれるとすぐに実行され、ページが送信されたかどうかを確認します。コメントアウトされた変数はもともとコードの一部でしたが、必要ないと判断したのでコメントアウトしました。元のコードは、フォームの結果を受信者にメールで送信することを目的としていましたが、フォームを送信するには「true を返す」必要があります。
<form action="https://crm.zoho.com/crm/WebToLeadForm" id="zoho1" method="POST" name="leadForm" onsubmit="return processForm()">
これは、アクションと onSubmit を含むフォームです。私が十分に明確であったことを願っています。助けてくれてありがとう。私は HTML 以外のプログラミングにかなり慣れていませんが、ほとんどの場合、すぐに習得できます。
また、理想的には、フォームの送信が成功したときに別の URL にリダイレクトします。