我在php中有这个联系表格,其中包含以下功能:名称(必填),电子邮件(必填),电话(不需要)和留言(必填)。
原始代码:
<?php
//contact form
function cs_theme_contact_form_shortcode($atts)
{
extract(shortcode_atts(array(
"id" => "contact_form",
"top_margin" => "none",
"el_class" => ""
), $atts));
$output = "";
$output .= '<form class="contact-form ' . ($top_margin!="none" ? esc_attr($top_margin) : '') . ($el_class!="" ? ' ' . esc_attr($el_class) : '') . '" id="' . esc_attr($id) . '" method="post" action="#">
<div class="vc_row wpb_row vc_inner">
<fieldset class="vc_col-sm-6 wpb_column vc_column_container">
<div class="block">
<input class="text_input" name="name" type="text" value="' . esc_html__("Your Name *", 'carservice') . '" placeholder="' . esc_html__("Your Name *", 'carservice') . '">
</div>
<div class="block">
<input class="text_input" name="email" type="text" value="' . esc_html__("Your Email *", 'carservice') . '" placeholder="' . esc_html__("Your Email *", 'carservice') . '">
</div>
<div class="block">
<input class="text_input" name="phone" type="text" value="' . esc_html__("Your Phone", 'carservice') . '" placeholder="' . esc_html__("Your Phone", 'carservice') . '">
</div>
</fieldset>
<fieldset class="vc_col-sm-6 wpb_column vc_column_container">
<div class="block">
<textarea class="margin_top_10" name="message" placeholder="' . esc_html__("Message *", 'carservice') . '">' . __("Message *", 'carservice') . '</textarea>
</div>
</fieldset>
</div>
<div class="vc_row wpb_row vc_inner margin-top-30">
<fieldset class="vc_col-sm-6 wpb_column vc_column_container">
<p>' . __("We will contact you within one business day.", 'carservice') . '</p>
</fieldset>
<fieldset class="vc_col-sm-6 wpb_column vc_column_container align-right">
<input type="hidden" name="action" value="theme_contact_form">
<input type="hidden" name="id" value="' . esc_attr($id) . '">
<div class="vc_row wpb_row vc_inner margin-top-20 padding-bottom-20">
<a class="more submit-contact-form" href="#" title="' . esc_html__("SEND MESSAGE", 'carservice') . '"><span>' . __("SEND MESSAGE", 'carservice') . '</span></a>
</div>
</fieldset>
</div>
</form>';
return $output;
}
add_shortcode("cs_contact_form", "cs_theme_contact_form_shortcode");
//visual composer
function cs_theme_contact_form_vc_init()
{
vc_map( array(
"name" => __("Contact form", 'carservice'),
"base" => "cs_contact_form",
"class" => "",
"controls" => "full",
"show_settings_on_create" => true,
"icon" => "icon-wpb-layer-contact-form",
"category" => __('Carservice', 'carservice'),
"params" => array(
array(
"type" => "textfield",
"class" => "",
"heading" => __("Id", 'carservice'),
"param_name" => "id",
"value" => "contact_form",
"description" => __("Please provide unique id for each contact form on the same page/post", 'carservice')
),
array(
"type" => "dropdown",
"class" => "",
"heading" => __("Top margin", 'carservice'),
"param_name" => "top_margin",
"value" => array(__("None", 'carservice') => "none", __("Page (small)", 'carservice') => "page-margin-top", __("Section (large)", 'carservice') => "page-margin-top-section")
),
array(
'type' => 'textfield',
'heading' => __( 'Extra class name', 'js_composer' ),
'param_name' => 'el_class',
'description' => __( 'If you wish to style particular content element differently, then use this field to add a class name and then refer to it in your css file.', 'js_composer' )
)
)
));
}
add_action("init", "cs_theme_contact_form_vc_init");
//contact form submit
function cs_theme_contact_form()
{
ob_start();
global $theme_options;
$result = array();
$result["isOk"] = true;
if($_POST["name"]!="" && $_POST["name"]!=__("Your Name *", 'carservice') && $_POST["email"]!="" && $_POST["email"]!=__("Your Email *", 'carservice') && preg_match("#^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$#", $_POST["email"]) && $_POST["message"]!="" && $_POST["message"]!=__("Message *", 'carservice'))
{
$values = array(
"name" => $_POST["name"],
"phone" => ($_POST["phone"]!=__("Your Phone", 'carservice') ? $_POST["phone"] : ""),
"email" => $_POST["email"],
"message" => $_POST["message"]
);
if((bool)ini_get("magic_quotes_gpc"))
$values = array_map("stripslashes", $values);
$values = array_map("htmlspecialchars", $values);
$headers[] = 'From: ' . $values["name"] . ' <' . $values["email"] . '>' . "\r\n";
$headers[] = 'Content-type: text/html';
$subject = $theme_options["cf_email_subject"];
$subject = str_replace("[name]", $values["name"], $subject);
$subject = str_replace("[email]", $values["email"], $subject);
$subject = str_replace("[phone]", $values["phone"], $subject);
$subject = str_replace("[message]", $values["message"], $subject);
$body = $theme_options["cf_template"];
$body = str_replace("[name]", $values["name"], $body);
$body = str_replace("[email]", $values["email"], $body);
$body = str_replace("[phone]", $values["phone"], $body);
$body = str_replace("[message]", $values["message"], $body);
$body = str_replace("[form_data]", "", $body);
if(wp_mail($theme_options["cf_admin_name"] . ' <' . $theme_options["cf_admin_email"] . '>', $subject, $body, $headers))
$result["submit_message"] = __("Thank you for contacting us", 'carservice');
else
{
$result["isOk"] = false;
$result["error_message"] = $GLOBALS['phpmailer']->ErrorInfo;
$result["submit_message"] = __("Sorry, we can't send this message", 'carservice');
}
}
else
{
$result["isOk"] = false;
if($_POST["name"]=="" || $_POST["name"]==__("Your Name *", 'carservice'))
$result["error_name"] = __("Please enter your name.", 'carservice');
if($_POST["email"]=="" || $_POST["email"]==__("Your Email *", 'carservice') || !preg_match("#^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$#", $_POST["email"]))
$result["error_email"] = __("Please enter valid e-mail.", 'carservice');
if($_POST["message"]=="" || $_POST["message"]==__("Message *", 'carservice'))
$result["error_message"] = __("Please enter your message.", 'carservice');
}
$system_message = ob_get_clean();
$result["system_message"] = $system_message;
echo @json_encode($result);
exit();
}
add_action("wp_ajax_theme_contact_form", "cs_theme_contact_form");
add_action("wp_ajax_nopriv_theme_contact_form", "cs_theme_contact_form");
?>
所以我想做一些更改并添加更多这样的功能:名称(必填),电子邮件(必填),电话(必填),地址(必填)和信息(不需要)。
然后我修改了这样的代码:
<?php
//contact form
function cs_theme_contact_form_shortcode($atts)
{
extract(shortcode_atts(array(
"id" => "contact_form",
"top_margin" => "none",
"el_class" => ""
), $atts));
$output = "";
$output .= '<form class="contact-form ' . ($top_margin!="none" ? esc_attr($top_margin) : '') . ($el_class!="" ? ' ' . esc_attr($el_class) : '') . '" id="' . esc_attr($id) . '" method="post" action="#">
<div class="vc_row wpb_row vc_inner">
<fieldset class="vc_col-sm-6 wpb_column vc_column_container">
<div class="block">
<input class="text_input" name="name" type="text" value="' . esc_html__("Nombre Completo *", 'carservice') . '" placeholder="' . esc_html__("Nombre Completo *", 'carservice') . '">
</div>
<div class="block">
<input class="text_input" name="email" type="text" value="' . esc_html__("Correo Electronico *", 'carservice') . '" placeholder="' . esc_html__("Correo Electronico *", 'carservice') . '">
</div>
<div class="block">
<input class="text_input" name="phone" type="text" value="' . esc_html__("Número de Celular *", 'carservice') . '" placeholder="' . esc_html__("Número de Celular *", 'carservice') . '">
</div>
<div class="block">
<input class="text_input" name="address" type="text" value="' . esc_html__("Dirección *", 'carservice') . '" placeholder="' . esc_html__("Dirección *", 'carservice') . '">
</div>
</fieldset>
<fieldset class="vc_col-sm-6 wpb_column vc_column_container">
<div class="block">
<textarea class="margin_top_10" name="message" placeholder="' . esc_html__("Mensaje", 'carservice') . '">' . __("Mensaje", 'carservice') . '</textarea>
</div>
</fieldset>
</div>
<div class="vc_row wpb_row vc_inner margin-top-30">
<fieldset class="vc_col-sm-6 wpb_column vc_column_container">
<p>' . __("Contactaremos contigo lo más pronto posible.", 'carservice') . '</p>
</fieldset>
<fieldset class="vc_col-sm-6 wpb_column vc_column_container align-right">
<input type="hidden" name="action" value="theme_contact_form">
<input type="hidden" name="id" value="' . esc_attr($id) . '">
<div class="vc_row wpb_row vc_inner margin-top-20 padding-bottom-20">
<a class="more submit-contact-form" href="#" title="' . esc_html__("ENVIAR", 'carservice') . '"><span>' . __("ENVIAR", 'carservice') . '</span></a>
</div>
</fieldset>
</div>
</form>';
return $output;
}
add_shortcode("cs_contact_form", "cs_theme_contact_form_shortcode");
//visual composer
function cs_theme_contact_form_vc_init()
{
vc_map( array(
"name" => __("Contact form", 'carservice'),
"base" => "cs_contact_form",
"class" => "",
"controls" => "full",
"show_settings_on_create" => true,
"icon" => "icon-wpb-layer-contact-form",
"category" => __('Carservice', 'carservice'),
"params" => array(
array(
"type" => "textfield",
"class" => "",
"heading" => __("Id", 'carservice'),
"param_name" => "id",
"value" => "contact_form",
"description" => __("Please provide unique id for each contact form on the same page/post", 'carservice')
),
array(
"type" => "dropdown",
"class" => "",
"heading" => __("Top margin", 'carservice'),
"param_name" => "top_margin",
"value" => array(__("None", 'carservice') => "none", __("Page (small)", 'carservice') => "page-margin-top", __("Section (large)", 'carservice') => "page-margin-top-section")
),
array(
'type' => 'textfield',
'heading' => __( 'Extra class name', 'js_composer' ),
'param_name' => 'el_class',
'description' => __( 'If you wish to style particular content element differently, then use this field to add a class name and then refer to it in your css file.', 'js_composer' )
)
)
));
}
add_action("init", "cs_theme_contact_form_vc_init");
//contact form submit
function cs_theme_contact_form()
{
ob_start();
global $theme_options;
$result = array();
$result["isOk"] = true;
if($_POST["name"]!="" && $_POST["name"]!=__("Nombre Completo *", 'carservice') && $_POST["email"]!="" && $_POST["email"]!=__("Correo Electronico *", 'carservice') && preg_match("#^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$#", $_POST["email"]) && $_POST["phone"]!="" && $_POST["phone"]!=__("Número de Celular", 'carservice') && preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{3}$/", $_POST["phone"]) && $_POST["address"]!="" && $_POST["address"]!=__("Dirección *", 'carservice'))
{
$values = array(
"name" => $_POST["name"],
"phone" => $_POST["phone"],
"email" => $_POST["email"],
"address" => $_POST["address"],
"message" => ($_POST["message"]!=__("Mensaje", 'carservice') ? $_POST["message"] : "")
);
if((bool)ini_get("magic_quotes_gpc"))
$values = array_map("stripslashes", $values);
$values = array_map("htmlspecialchars", $values);
$headers[] = 'From: ' . $values["name"] . ' <' . $values["email"] . '>' . "\r\n";
$headers[] = 'Content-type: text/html';
$subject = str_replace("[name]", $values["name"], $subject);
$subject = str_replace("[email]", $values["email"], $subject);
$subject = str_replace("[phone]", $values["phone"], $subject);
$subject = str_replace("[address]", $values["address"], $subject);
$subject = str_replace("[message]", $values["message"], $subject);
$body = $theme_options["cf_template"];
$body = str_replace("[name]", $values["name"], $body);
$body = str_replace("[email]", $values["email"], $body);
$body = str_replace("[phone]", $values["phone"], $body);
$body = str_replace("[address]", $values["address"], $body);
$body = str_replace("[message]", $values["message"], $body);
$body = str_replace("[form_data]", "", $body);
if(wp_mail($theme_options["cf_admin_name"] . ' <' . $theme_options["cf_admin_email"] . '>', $subject, $body, $headers))
$result["submit_message"] = __("Gracias por preferirnos. :)", 'carservice');
else
{
$result["isOk"] = false;
$result["error_message"] = $GLOBALS['phpmailer']->ErrorInfo;
$result["submit_message"] = __("Lo siento, no podemos enviar el mensaje por el momento. :()", 'carservice');
}
}
else
{
$result["isOk"] = false;
if($_POST["name"]=="" || $_POST["name"]==__("Nombre Completo *", 'carservice'))
$result["error_name"] = __("Por favor ingrese su nombre completo.", 'carservice');
if($_POST["email"]=="" || $_POST["email"]==__("Correo Electronico *", 'carservice') || !preg_match("#^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$#", $_POST["email"]))
$result["error_email"] = __("Por favor ingresa un correo electronico válido.", 'carservice');
if($_POST["phone"]=="" || $_POST["phone"]==__("Número de Celular *", 'carservice') || !preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{3}$/", $_POST["phone"]))
$result["error_phone"] = __("Por favor ingresa un número de celular válido.", 'carservice');
if($_POST["address"]=="" || $_POST["address"]==__("Dirección *", 'carservice'))
$result["error_address"] = __("Por favor tu dirección.", 'carservice');
}
$system_message = ob_get_clean();
$result["system_message"] = $system_message;
echo @json_encode($result);
exit();
}
add_action("wp_ajax_theme_contact_form", "cs_theme_contact_form");
add_action("wp_ajax_nopriv_theme_contact_form", "cs_theme_contact_form");
?>
我想要做的是添加一些这样的限制: 必须填写姓名,如果没有,则会显示一条消息“请写下您的姓名”;与电子邮件相同的事情(都在原始联系表格和修改后的代码中工作)。我用手机尝试了同样的事情(手机必须只包含数字,否则会出现一些消息说“请写一个有效的数字”,但是当我测试并写“98231176a”时,消息没有出现;同样的错误发生了有了地址,显然所有信息都没有发送到管理员电子邮箱,因为手机无效。 我对php知之甚少,如果有人可以帮助我的话。感谢
答案 0 :(得分:1)
替换:
&& preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{3}$/", $_POST["phone"])
使用:
&& is_numeric($_POST['phone']);
还有:
|| !preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{3}$/", $_POST["phone"])
with:
|| !is_numeric($_POST['phone']);
你也可以在里面创建一个函数:functions.php并将电话号码传递给函数并获得真或假,以换取它是否有效的电话号码:
function validatePhone($string) {
$numbersOnly = ereg_replace("[^0-9]", "", $string);
$numberOfDigits = strlen($numbersOnly);
if ($numberOfDigits == 7 or $numberOfDigits == 10) {
return true;
} else {
return false;
}
}
使用它:
validatePhone($_POST['phone'])
它将解决电话号码的问题,并使重要的是数字,不包含任何字母
完整代码:
<?php
//contact form
function cs_theme_contact_form_shortcode($atts)
{
extract(shortcode_atts(array(
"id" => "contact_form",
"top_margin" => "none",
"el_class" => ""
), $atts));
$output = "";
$output .= '<form class="contact-form ' . ($top_margin!="none" ? esc_attr($top_margin) : '') . ($el_class!="" ? ' ' . esc_attr($el_class) : '') . '" id="' . esc_attr($id) . '" method="post" action="#">
<div class="vc_row wpb_row vc_inner">
<fieldset class="vc_col-sm-6 wpb_column vc_column_container">
<div class="block">
<input class="text_input" name="name" type="text" value="' . esc_html__("Nombre Completo *", 'carservice') . '" placeholder="' . esc_html__("Nombre Completo *", 'carservice') . '">
</div>
<div class="block">
<input class="text_input" name="email" type="text" value="' . esc_html__("Correo Electronico *", 'carservice') . '" placeholder="' . esc_html__("Correo Electronico *", 'carservice') . '">
</div>
<div class="block">
<input class="text_input" name="phone" type="text" value="' . esc_html__("Número de Celular *", 'carservice') . '" placeholder="' . esc_html__("Número de Celular *", 'carservice') . '">
</div>
<div class="block">
<input class="text_input" name="address" type="text" value="' . esc_html__("Dirección *", 'carservice') . '" placeholder="' . esc_html__("Dirección *", 'carservice') . '">
</div>
</fieldset>
<fieldset class="vc_col-sm-6 wpb_column vc_column_container">
<div class="block">
<textarea class="margin_top_10" name="message" placeholder="' . esc_html__("Mensaje", 'carservice') . '">' . __("Mensaje", 'carservice') . '</textarea>
</div>
</fieldset>
</div>
<div class="vc_row wpb_row vc_inner margin-top-30">
<fieldset class="vc_col-sm-6 wpb_column vc_column_container">
<p>' . __("Contactaremos contigo lo más pronto posible.", 'carservice') . '</p>
</fieldset>
<fieldset class="vc_col-sm-6 wpb_column vc_column_container align-right">
<input type="hidden" name="action" value="theme_contact_form">
<input type="hidden" name="id" value="' . esc_attr($id) . '">
<div class="vc_row wpb_row vc_inner margin-top-20 padding-bottom-20">
<a class="more submit-contact-form" href="#" title="' . esc_html__("ENVIAR", 'carservice') . '"><span>' . __("ENVIAR", 'carservice') . '</span></a>
</div>
</fieldset>
</div>
</form>';
return $output;
}
add_shortcode("cs_contact_form", "cs_theme_contact_form_shortcode");
//visual composer
function cs_theme_contact_form_vc_init()
{
vc_map( array(
"name" => __("Contact form", 'carservice'),
"base" => "cs_contact_form",
"class" => "",
"controls" => "full",
"show_settings_on_create" => true,
"icon" => "icon-wpb-layer-contact-form",
"category" => __('Carservice', 'carservice'),
"params" => array(
array(
"type" => "textfield",
"class" => "",
"heading" => __("Id", 'carservice'),
"param_name" => "id",
"value" => "contact_form",
"description" => __("Please provide unique id for each contact form on the same page/post", 'carservice')
),
array(
"type" => "dropdown",
"class" => "",
"heading" => __("Top margin", 'carservice'),
"param_name" => "top_margin",
"value" => array(__("None", 'carservice') => "none", __("Page (small)", 'carservice') => "page-margin-top", __("Section (large)", 'carservice') => "page-margin-top-section")
),
array(
'type' => 'textfield',
'heading' => __( 'Extra class name', 'js_composer' ),
'param_name' => 'el_class',
'description' => __( 'If you wish to style particular content element differently, then use this field to add a class name and then refer to it in your css file.', 'js_composer' )
)
)
));
}
add_action("init", "cs_theme_contact_form_vc_init");
//contact form submit
function cs_theme_contact_form()
{
ob_start();
global $theme_options;
$result = array();
$result["isOk"] = true;
if($_POST["name"]!="" && $_POST["name"]!=__("Nombre Completo *", 'carservice') && $_POST["email"]!="" && $_POST["email"]!=__("Correo Electronico *", 'carservice') && preg_match("#^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$#", $_POST["email"]) && $_POST["phone"]!="" && $_POST["phone"]!=__("Número de Celular", 'carservice') && is_numeric($_POST['phone']) && $_POST["address"]!="" && $_POST["address"]!=__("Dirección *", 'carservice'))
{
$values = array(
"name" => $_POST["name"],
"phone" => $_POST["phone"],
"email" => $_POST["email"],
"address" => $_POST["address"],
"message" => ($_POST["message"]!=__("Mensaje", 'carservice') ? $_POST["message"] : "")
);
if((bool)ini_get("magic_quotes_gpc"))
$values = array_map("stripslashes", $values);
$values = array_map("htmlspecialchars", $values);
$headers[] = 'From: ' . $values["name"] . ' <' . $values["email"] . '>' . "\r\n";
$headers[] = 'Content-type: text/html';
$subject = str_replace("[name]", $values["name"], $subject);
$subject = str_replace("[email]", $values["email"], $subject);
$subject = str_replace("[phone]", $values["phone"], $subject);
$subject = str_replace("[address]", $values["address"], $subject);
$subject = str_replace("[message]", $values["message"], $subject);
$body = $theme_options["cf_template"];
$body = str_replace("[name]", $values["name"], $body);
$body = str_replace("[email]", $values["email"], $body);
$body = str_replace("[phone]", $values["phone"], $body);
$body = str_replace("[address]", $values["address"], $body);
$body = str_replace("[message]", $values["message"], $body);
$body = str_replace("[form_data]", "", $body);
if(wp_mail($theme_options["cf_admin_name"] . ' <' . $theme_options["cf_admin_email"] . '>', $subject, $body, $headers))
$result["submit_message"] = __("Gracias por preferirnos. :)", 'carservice');
else
{
$result["isOk"] = false;
$result["error_message"] = $GLOBALS['phpmailer']->ErrorInfo;
$result["submit_message"] = __("Lo siento, no podemos enviar el mensaje por el momento. :()", 'carservice');
}
}
else
{
$result["isOk"] = false;
if($_POST["name"]=="" || $_POST["name"]==__("Nombre Completo *", 'carservice'))
$result["error_name"] = __("Por favor ingrese su nombre completo.", 'carservice');
if($_POST["email"]=="" || $_POST["email"]==__("Correo Electronico *", 'carservice') || !preg_match("#^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$#", $_POST["email"]))
$result["error_email"] = __("Por favor ingresa un correo electronico válido.", 'carservice');
if($_POST["phone"]=="" || $_POST["phone"]==__("Número de Celular *", 'carservice') || !is_numeric($_POST['phone']))
$result["error_phone"] = __("Por favor ingresa un número de celular válido.", 'carservice');
if($_POST["address"]=="" || $_POST["address"]==__("Dirección *", 'carservice'))
$result["error_address"] = __("Por favor tu dirección.", 'carservice');
}
$system_message = ob_get_clean();
$result["system_message"] = $system_message;
echo @json_encode($result);
exit();
}
add_action("wp_ajax_theme_contact_form", "cs_theme_contact_form");
add_action("wp_ajax_nopriv_theme_contact_form", "cs_theme_contact_form");
?>