改进并应用更好的块代码格式

Improve and apply a better block code format

本文关键字:代码 格式 更好 应用      更新时间:2023-09-26

HTML在页面上:

<form>
<input type="hidden" value="New Message" name="New Message" id="subject">
    <div class="col-md-3">
        <label for="name">Your Name: </label>
        <span class="your-name">
        <input type="text" id="name" size="40" aria-required="true" aria-invalid="false"></span>
    </div>
    <div class="col-md-3">
        <label for="email">E-Mail: </label>
        <input type="email" id="email">
    </div>
    <div class="col-md-3">
        <label for="message">Tell Us Everything: </label>
        <textarea cols="40" rows="1" name="message" id="message" ></textarea>
    </div>
    <div class="col-md-3">
        <div id="button-con">
            <input type="submit" value="Submit" id="submit" onClick="return check_values();">
            <img src="ajax-loader.gif" alt="Sending ..." style="visibility: hidden;" />
        </div>
    </div>
</form>

js文件:

// JavaScript Document
var http = createRequestObject();
var areal = Math.random() + "";
var real = areal.substring(2,6);
function createRequestObject() {
    var xmlhttp;
    try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
  catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
    catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
    xmlhttp=new XMLHttpRequest();
  }
    return  xmlhttp;
}
function sendRequest() {
    var rnd = Math.random();
    var name = escape(document.getElementById("name").value);
    var email = escape(document.getElementById("email").value);
    var subject = escape(document.getElementById("subject").value);
    var message = escape(document.getElementById("message").value);
    try{
    http.open('POST',  'http://my-domain.com/form.php');
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.onreadystatechange = handleResponse;
        http.send('$name='+name+'&email='+email+'&subject='+subject+'&message='+message+'&rnd='+rnd);
    }
    catch(e){}
    finally{}
}
function check_values() {
    var valid = '';
    var name = document.getElementById("name").value;
    var email = document.getElementById("email").value;
    var subject = document.getElementById("subject").value;
    var body = document.getElementById("message").value;
    if(trim(name) == "" ||
        trim(email) == "" ||
        trim(subject) == "" ||
        trim(body) == "") {
            alert("Please complete all fields");
    } else {
        if(isEmail(email)) {
            document.getElementById("submit").disabled=true;
            document.getElementById("submit").value='Please Wait..';
            sendRequest();
        } else {
            alert("Email appears to be invalid'nPlease check and try again");
            document.getElementById("email").focus();
            document.getElementById("email").select();
        }
    }
}
function handleResponse() {
    try{
    if((http.readyState == 4)&&(http.status == 200)){
        var response = http.responseText;
      document.getElementById("confirmation").innerHTML = response;
      document.getElementById("confirmation").style.display ="";
        }
  }
    catch(e){}
    finally{}
}
function isUndefined(a) {
   return typeof a == 'undefined';
}
function trim(a) {
    return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}
function isEmail(a) {
   return (a.indexOf(".") > 0) && (a.indexOf("@") > 0);
}

PHP:

<?php
 error_reporting(0);
  $page_title = "Contact Us Form";
  $email_it_to = "test@test.com";
  $error_message = "Please complete the form first";
  $confirmation = "Thank you, your message has been successfully sent.";
?>

<?php
error_reporting(0);
include 'blah.com/form_config.php';
if(!isset($_POST['rnd']) || !isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['subject']) || !isset($_POST['message'])) {
    echo $error_message;
    die();
}
    $email_from = $email;
    $email_subject = "Contact Form: ".stripslashes($subject);
    $email_message = "Please find below a message submitted by '".stripslashes($name);
    $email_message .="' on ".date("d/m/Y")." at ".date("H:i")."'n'n";
    $email_message .= stripslashes($message);
    $headers = 'From: '.$email_from."'r'n" .
   'Reply-To: '.$email_from."'r'n" .
   'X-Mailer: PHP/' . phpversion();
    mail($email_it_to, $email_subject, $email_message, $headers);
    echo "<b>$confirmation</b>";
    die();
?>

代码一直在更改提交按钮上的文本,但没有发送-我们非常感谢您的帮助。

我似乎找不到错误-我添加和删除了一些尝试和错误的东西,但似乎无法获得100%

看起来像一个简单的打字错误:http.send('$name=...

你有$,而你应该有一个安培数和&

在javascript中将其更改为http.send('&name=(在 sendRequest()函数中的try{}中)应该会保留

if(!isset($_POST['rnd']) || !isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['subject']) || !isset($_POST['message'])) {
    echo $error_message;
    die();
}

由于$_POST['name']实际上是集,因此它被认为是querystring的部分,然后将执行其余的mailer代码。

您只需要更改以下行:

http.onreadystatechange=句柄响应
http.send('name='+name+'&email='+email+'&aamp;subject='+subject+'&amph;message='+message+'&rnd='+rnd)
//在发送name变量的值时,从name中删除$。}

我修复了它,正如Marcus所建议的那样,我只是忽略了js试图发布的内容,以下是所需的代码:

$email_from = ($_POST['email']);
$email_subject = "Contact Form: ".($_POST['subject']);
$email_name = "Client Name: ".($_POST['name']);
$email_message = "From Email: ".($_POST['email']);
$email_message = "Their Message: ".($_POST['message']);

mail($email_it_to, $email_subject, $email_name, $email_message, $headers);

很抱歉延迟回复,如果问题得到解决,那就好了。

但在我看来,使用ajax的一个简单方法是:

$.ajax({
type:'post',
data:'&task=asdasd',
url:'ajax.php',
success:function(response){
alert(response);
}
})

希望这能节省时间。