佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1805|回复: 25

请问怎样在submit form之前跳出confimation box??

[复制链接]
发表于 26-9-2015 02:32 PM | 显示全部楼层 |阅读模式
这是submit的按钮,请问怎样在submit form之前跳出confimation box
我想加这个
http://www.w3schools.com/jsref/t ... me=tryjsref_confirm
1.png

这是submit button
  1. <div class='container'>
  2.     <input type='submit' name='Submit' value='Submit' />
  3. </div>
复制代码


这是confirmation pop up
  1. <html>
  2. <body>

  3. <p>Click the button to display a confirm box.</p>

  4. <button onclick="myFunction()">Try it</button>

  5. <script>
  6. function myFunction() {
  7.     confirm("Press a button!");
  8. }
  9. </script>

  10. </body>
  11. </html>
复制代码
应该如何加进去才能实现在confirmation box按OK就SUBMIT,按Cancel就取消submit form?
或者有没有更简单的方法??

我只是初学者。
回复

使用道具 举报


ADVERTISEMENT

发表于 26-9-2015 03:14 PM | 显示全部楼层
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title></title>
  5. </head>
  6. <body>
  7.     <form action="HtmlPage.html" onsubmit="onSubmit(event)">
  8.         <input type="text" value="keatkeat" />
  9.         <input type="submit" />
  10.     </form>
  11.     <script>
  12.         function onSubmit(e) {            
  13.             if (!confirm("sure bo ? you want submit meh ?")) {
  14.                 e.preventDefault();  //阻止事件进行
  15.             }
  16.         }
  17.     </script>
  18. </body>

  19. </html>
复制代码


拦截 onSubmit 事件.
当然方法很多啦.
去问老师吧。
如果是自学没老师问的话,那先去找个老师吧。

回复

使用道具 举报

发表于 26-9-2015 03:16 PM | 显示全部楼层
  1. <form  method="post" onsubmit="return confirm('Are you sure you want to submit?');">

  2. <input type='submit' name='Submit' value='Submit' />

  3. </form>
复制代码
回复

使用道具 举报

发表于 26-9-2015 09:35 PM | 显示全部楼层
本帖最后由 musicalangel 于 26-9-2015 10:27 PM 编辑

首先你要分辨出 html 裡面 button 跟 submit 是兩個完全不同的東西,
最大的差別是, submit 必須跟 form 的tag裡面, 然後 submit 一但按下後, form 就會送出,

而button 可以單獨存在, 然後用event handler 來指定他被按下後, 要執行甚麼動作.

我先顯示以 submit 為基礎的code

HTML:
  1. <form id="form1" name="form1" method="POST">
  2.        <input type="submit" id="submit_btn" name="submit_btn" value="Send">
  3. </form>
复制代码

Javascript:
  1. $(function() {
  2.      $("#form1").submit(function(e){ /*綁定form1在執行 submit的時候要執行以下的動作 */
  3.                 if(!confirm("Press a button"))  /* 如果在 confirm box 裡面按了Cancel */
  4.           {
  5.                  e.preventDefault();  /* 取消掉form1 的 submit*/
  6.           }
  7.      });
  8. });
复制代码

PS:你必須注意, 這個example 我有使用JQuery

你可以在這裡測試:
https://jsfiddle.net/g9tqckgL/

如果你還是面對著submit 時沒有辦法停下submit
有幾方面你是要注意的

  • 你的停止submit event 的語法不對 例如上面所顯示的 e.preventDefault
  • 或者是你的comfirm沒有寫對, 例如少了個 驚嘆號(!)
  • 你把submit 跟 button 搞錯了.

我建議你把你的code 帖出來, 這樣大家比較容以幫到你.
祝學習愉快.


回复

使用道具 举报

 楼主| 发表于 26-9-2015 09:47 PM | 显示全部楼层

我在这里测试可以
http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert
可是放在form就阻拦不了,为什么??

回复

使用道具 举报

 楼主| 发表于 26-9-2015 09:47 PM | 显示全部楼层
keatkeat87 发表于 26-9-2015 03:14 PM
拦截 onSubmit 事件.
当然方法很多啦.
去问老师吧。

我在这里测试可以
http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert
可是放在form就阻拦不了,为什么??
回复

使用道具 举报

Follow Us
 楼主| 发表于 27-9-2015 03:20 AM | 显示全部楼层
本帖最后由 chew2293 于 27-9-2015 03:27 AM 编辑
musicalangel 发表于 26-9-2015 09:35 PM
首先你要分辨出 html 裡面 button 跟 submit 是兩個完全不同的東西,
最大的差別是, submit 必須跟 form  ...

这是form
  1. <?PHP
  2. /*
  3.     Contact Form from HTML Form Guide
  4.     This program is free software published under the
  5.     terms of the GNU Lesser General Public License.
  6.     See this page for more info:
  7.     http://www.html-form-guide.com/contact-form/contact-form-attachment.html
  8. */
  9. require_once("./include/fgcontactform.php");

  10. $formproc = new FGContactForm();

  11. //1. Add your email address here.
  12. //You can add more than one receipients.
  13. $formproc->AddRecipient('tests@html-form-guide.com'); //<<---Put your email address here


  14. //2. For better security. Get a random tring from this link: http://tinyurl.com/randstr
  15. // and put it here
  16. $formproc->SetFormRandomKey('HG9hPBpn9Bn26yg');

  17. $formproc->AddFileUploadField('photo','jpg,jpeg,gif,png,bmp',2024);

  18. if(isset($_POST['submitted']))
  19. {
  20.    if($formproc->ProcessForm())
  21.    {
  22.         $formproc->RedirectToURL("thank-you.php");
  23.    }
  24. }

  25. ?>
  26. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  27. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
  28. <head>
  29.       <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
  30.       <title>Contact us</title>
  31.       <link rel="STYLESHEET" type="text/css" href="contact.css" />
  32.       <script type='text/javascript' src='scripts/gen_validatorv31.js'></script>
  33.       <script type='text/javascript' src='scripts/fg_captcha_validator.js'></script>
  34. </head>
  35. <body>

  36. <!-- Form Code Start -->
  37. <form id='contactus' action='<?php echo $formproc->GetSelfScript(); ?>' method='post' enctype="multipart/form-data" accept-charset='UTF-8'>

  38. <fieldset >
  39. <legend>Contact us</legend>

  40. <input type='hidden' name='submitted' id='submitted' value='1'/>
  41. <input type='hidden' name='<?php echo $formproc->GetFormIDInputName(); ?>' value='<?php echo $formproc->GetFormIDInputValue(); ?>'/>
  42. <input type='text'  class='spmhidip' name='<?php echo $formproc->GetSpamTrapInputName(); ?>' />

  43. <div class='short_explanation'>* required fields</div>

  44. <div><span class='error'><?php echo $formproc->GetErrorMessage(); ?></span></div>
  45. <div class='container'>
  46.     <label for='name' >Your Full Name*: </label><br/>
  47.     <input type='text' name='name' id='name' value='<?php echo $formproc->SafeDisplay('name') ?>' maxlength="50" /><br/>
  48.     <span id='contactus_name_errorloc' class='error'></span>
  49. </div>
  50. <div class='container'>
  51.     <label for='email' >Email Address*:</label><br/>
  52.     <input type='text' name='email' id='email' value='<?php echo $formproc->SafeDisplay('email') ?>' maxlength="50" /><br/>
  53.     <span id='contactus_email_errorloc' class='error'></span>
  54. </div>
  55. <div class='container'>
  56.     <label for='message' >Message:</label><br/>
  57.     <span id='contactus_message_errorloc' class='error'></span>
  58.     <textarea rows="10" cols="50" name='message' id='message'><?php echo $formproc->SafeDisplay('message') ?></textarea>
  59. </div>
  60. <div class='container'>
  61.     <label for='photo' >Upload your photo:</label><br/>
  62.     <input type="file" name='photo' id='photo' /><br/>
  63.     <span id='contactus_photo_errorloc' class='error'></span>
  64. </div>


  65. <!--submit 在这里-->
  66. <div class='container'>
  67.     <input type='submit' name='Submit' value='Submit' />
  68. </div>

  69. </fieldset>
  70. </form>
  71. <!-- client-side Form Validations:
  72. Uses the excellent form validation script from JavaScript-coder.com-->

  73. <script type='text/javascript'>
  74. // <![CDATA[

  75.     var frmvalidator  = new Validator("contactus");
  76.     frmvalidator.EnableOnPageErrorDisplay();
  77.     frmvalidator.EnableMsgsTogether();
  78.     frmvalidator.addValidation("name","req","Please provide your name");

  79.     frmvalidator.addValidation("email","req","Please provide your email address");

  80.     frmvalidator.addValidation("email","email","Please provide a valid email address");

  81.     frmvalidator.addValidation("message","maxlen=2048","The message is too long!(more than 2KB!)");

  82.     frmvalidator.addValidation("photo","file_extn=jpg;jpeg;gif;png;bmp","Upload images only. Supported file types are: jpg,gif,png,bmp");
  83. // ]]>
  84. </script>
  85. </body>
  86. </html>
复制代码

还有这个是有连接form的,从提交资料到email都有关系的,不懂有没有关系,不过也分享出来吧
  1. <?PHP
  2. /*
  3.     Contact Form from HTML Form Guide

  4.     This program is free software published under the
  5.     terms of the GNU Lesser General Public License.

  6. This program is distributed in the hope that it will
  7. be useful - WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A
  9. PARTICULAR PURPOSE.

  10. [url=home.php?mod=space&uid=615991]@copyright[/url] html-form-guide.com 2010
  11. */
  12. require_once("class.phpmailer.php");

  13. /*
  14. Interface to Captcha handler
  15. */
  16. class FG_CaptchaHandler
  17. {
  18.     function Validate() { return false;}
  19.     function GetError(){ return '';}
  20. }
  21. /*
  22. FGContactForm is a general purpose contact form class
  23. It supports Captcha, HTML Emails, sending emails
  24. conditionally, File atachments and more.
  25. */
  26. class FGContactForm
  27. {
  28.     var $receipients;
  29.     var $errors;
  30.     var $error_message;
  31.     var $name;
  32.     var $email;
  33.     var $message;
  34.     var $from_address;
  35.     var $form_random_key;
  36.     var $conditional_field;
  37.     var $arr_conditional_receipients;
  38.     var $fileupload_fields;
  39.     var $captcha_handler;

  40.     var $mailer;

  41.     function FGContactForm()
  42.     {
  43.         $this->receipients = array();
  44.         $this->errors = array();
  45.         $this->form_random_key = 'HTgsjhartag';
  46.         $this->conditional_field='';
  47.         $this->arr_conditional_receipients=array();
  48.         $this->fileupload_fields=array();

  49.         $this->mailer = new PHPMailer();
  50.         $this->mailer->CharSet = 'utf-8';
  51.     }

  52.     function EnableCaptcha($captcha_handler)
  53.     {
  54.         $this->captcha_handler = $captcha_handler;
  55.         session_start();
  56.     }

  57.     function AddRecipient($email,$name="")
  58.     {
  59.         $this->mailer->AddAddress($email,$name);
  60.     }

  61.     function SetFromAddress($from)
  62.     {
  63.         $this->from_address = $from;
  64.     }
  65.     function SetFormRandomKey($key)
  66.     {
  67.         $this->form_random_key = $key;
  68.     }
  69.     function GetSpamTrapInputName()
  70.     {
  71.         return 'sp'.md5('KHGdnbvsgst'.$this->GetKey());
  72.     }
  73.     function SafeDisplay($value_name)
  74.     {
  75.         if(empty($_POST[$value_name]))
  76.         {
  77.             return'';
  78.         }
  79.         return htmlentities($_POST[$value_name]);
  80.     }
  81.     function GetFormIDInputName()
  82.     {
  83.         $rand = md5('TygshRt'.$this->GetKey());

  84.         $rand = substr($rand,0,20);
  85.         return 'id'.$rand;
  86.     }


  87.     function GetFormIDInputValue()
  88.     {
  89.         return md5('jhgahTsajhg'.$this->GetKey());
  90.     }

  91.     function SetConditionalField($field)
  92.     {
  93.         $this->conditional_field = $field;
  94.     }
  95.     function AddConditionalReceipent($value,$email)
  96.     {
  97.         $this->arr_conditional_receipients[$value] =  $email;
  98.     }

  99.     function AddFileUploadField($file_field_name,$accepted_types,$max_size)
  100.     {

  101.         $this->fileupload_fields[] =
  102.             array("name"=>$file_field_name,
  103.             "file_types"=>$accepted_types,
  104.             "maxsize"=>$max_size);
  105.     }

  106.     function ProcessForm()
  107.     {
  108.         if(!isset($_POST['submitted']))
  109.         {
  110.            return false;
  111.         }
  112.         if(!$this->Validate())
  113.         {
  114.             $this->error_message = implode('<br/>',$this->errors);
  115.             return false;
  116.         }
  117.         $this->CollectData();

  118.         $ret = $this->SendFormSubmission();

  119.         return $ret;
  120.     }

  121.     function RedirectToURL($url)
  122.     {
  123.         header("Location: $url");
  124.         exit;
  125.     }

  126.     function GetErrorMessage()
  127.     {
  128.         return $this->error_message;
  129.     }
  130.     function GetSelfScript()
  131.     {
  132.         return htmlentities($_SERVER['PHP_SELF']);
  133.     }

  134.     function GetName()
  135.     {
  136.         return $this->name;
  137.     }
  138.     function GetEmail()
  139.     {
  140.         return $this->email;
  141.     }
  142.     function GetMessage()
  143.     {
  144.         return htmlentities($this->message,ENT_QUOTES,"UTF-8");
  145.     }

  146. /*--------  Private (Internal) Functions -------- */


  147.     function SendFormSubmission()
  148.     {
  149.         $this->CollectConditionalReceipients();

  150.         $this->mailer->CharSet = 'utf-8';
  151.         
  152.         $this->mailer->Subject = "Contact form submission from $this->name";

  153.         $this->mailer->From = $this->GetFromAddress();

  154.         $this->mailer->FromName = $this->name;

  155.         $this->mailer->AddReplyTo($this->email);

  156.         $message = $this->ComposeFormtoEmail();

  157.         $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message)));
  158.         $this->mailer->AltBody = @html_entity_decode($textMsg,ENT_QUOTES,"UTF-8");
  159.         $this->mailer->MsgHTML($message);

  160.         $this->AttachFiles();

  161.         if(!$this->mailer->Send())
  162.         {
  163.             $this->add_error("Failed sending email!");
  164.             return false;
  165.         }

  166.         return true;
  167.     }

  168.     function CollectConditionalReceipients()
  169.     {
  170.         if(count($this->arr_conditional_receipients)>0 &&
  171.           !empty($this->conditional_field) &&
  172.           !empty($_POST[$this->conditional_field]))
  173.         {
  174.             foreach($this->arr_conditional_receipients as $condn => $rec)
  175.             {
  176.                 if(strcasecmp($condn,$_POST[$this->conditional_field])==0 &&
  177.                 !empty($rec))
  178.                 {
  179.                     $this->AddRecipient($rec);
  180.                 }
  181.             }
  182.         }
  183.     }

  184.     /*
  185.     Internal variables, that you donot want to appear in the email
  186.     Add those variables in this array.
  187.     */
  188.     function IsInternalVariable($varname)
  189.     {
  190.         $arr_interanl_vars = array('scaptcha',
  191.                             'submitted',
  192.                             $this->GetSpamTrapInputName(),
  193.                             $this->GetFormIDInputName()
  194.                             );
  195.         if(in_array($varname,$arr_interanl_vars))
  196.         {
  197.             return true;
  198.         }
  199.         return false;
  200.     }

  201.     function FormSubmissionToMail()
  202.     {
  203.         $ret_str='';
  204.         foreach($_POST as $key=>$value)
  205.         {
  206.             if(!$this->IsInternalVariable($key))
  207.             {
  208.                 $value = htmlentities($value,ENT_QUOTES,"UTF-8");
  209.                 $value = nl2br($value);
  210.                 $key = ucfirst($key);
  211.                 $ret_str .= "<div class='label'>$key :</div><div class='value'>$value </div>\n";
  212.             }
  213.         }
  214.         foreach($this->fileupload_fields as $upload_field)
  215.         {
  216.             $field_name = $upload_field["name"];
  217.             if(!$this->IsFileUploaded($field_name))
  218.             {
  219.                 continue;
  220.             }        
  221.             
  222.             $filename = basename($_FILES[$field_name]['name']);

  223.             $ret_str .= "<div class='label'>File upload '$field_name' :</div><div class='value'>$filename </div>\n";
  224.         }
  225.         return $ret_str;
  226.     }

  227.     function ExtraInfoToMail()
  228.     {
  229.         $ret_str='';

  230.         $ip = $_SERVER['REMOTE_ADDR'];
  231.         $ret_str = "<div class='label'>IP address of the submitter:</div><div class='value'>$ip</div>\n";

  232.         return $ret_str;
  233.     }

  234.     function GetMailStyle()
  235.     {
  236.         $retstr = "\n<style>".
  237.         "body,.label,.value { font-family:Arial,Verdana; } ".
  238.         ".label {font-weight:bold; margin-top:5px; font-size:1em; color:#333;} ".
  239.         ".value {margin-bottom:15px;font-size:0.8em;padding-left:5px;} ".
  240.         "</style>\n";

  241.         return $retstr;
  242.     }
  243.     function GetHTMLHeaderPart()
  244.     {
  245.          $retstr = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'."\n".
  246.                    '<html><head><title></title>'.
  247.                    '<meta http-equiv=Content-Type content="text/html; charset=utf-8">';
  248.          $retstr .= $this->GetMailStyle();
  249.          $retstr .= '</head><body>';
  250.          return $retstr;
  251.     }
  252.     function GetHTMLFooterPart()
  253.     {
  254.         $retstr ='</body></html>';
  255.         return $retstr ;
  256.     }
  257.     function ComposeFormtoEmail()
  258.     {
  259.         $header = $this->GetHTMLHeaderPart();
  260.         $formsubmission = $this->FormSubmissionToMail();
  261.         $extra_info = $this->ExtraInfoToMail();
  262.         $footer = $this->GetHTMLFooterPart();

  263.         $message = $header."Submission from 'contact us' form:<p>$formsubmission</p><hr/>$extra_info".$footer;

  264.         return $message;
  265.     }

  266.     function AttachFiles()
  267.     {
  268.         foreach($this->fileupload_fields as $upld_field)
  269.         {
  270.             $field_name = $upld_field["name"];
  271.             if(!$this->IsFileUploaded($field_name))
  272.             {
  273.                 continue;
  274.             }
  275.             
  276.             $filename =basename($_FILES[$field_name]['name']);

  277.             $this->mailer->AddAttachment($_FILES[$field_name]["tmp_name"],$filename);
  278.         }
  279.     }

  280.     function GetFromAddress()
  281.     {
  282.         if(!empty($this->from_address))
  283.         {
  284.             return $this->from_address;
  285.         }

  286.         $host = $_SERVER['SERVER_NAME'];

  287.         $from ="nobody@$host";
  288.         return $from;
  289.     }

  290.     function Validate()
  291.     {
  292.         $ret = true;
  293.         //security validations
  294.         if(empty($_POST[$this->GetFormIDInputName()]) ||
  295.           $_POST[$this->GetFormIDInputName()] != $this->GetFormIDInputValue() )
  296.         {
  297.             //The proper error is not given intentionally
  298.             $this->add_error("Automated submission prevention: case 1 failed");
  299.             $ret = false;
  300.         }

  301.         //This is a hidden input field. Humans won't fill this field.
  302.         if(!empty($_POST[$this->GetSpamTrapInputName()]) )
  303.         {
  304.             //The proper error is not given intentionally
  305.             $this->add_error("Automated submission prevention: case 2 failed");
  306.             $ret = false;
  307.         }

  308.         //name validations
  309.         if(empty($_POST['name']))
  310.         {
  311.             $this->add_error("Please provide your name");
  312.             $ret = false;
  313.         }
  314.         else
  315.         if(strlen($_POST['name'])>50)
  316.         {
  317.             $this->add_error("Name is too big!");
  318.             $ret = false;
  319.         }

  320.         //email validations
  321.         if(empty($_POST['email']))
  322.         {
  323.             $this->add_error("Please provide your email address");
  324.             $ret = false;
  325.         }
  326.         else
  327.         if(strlen($_POST['email'])>50)
  328.         {
  329.             $this->add_error("Email address is too big!");
  330.             $ret = false;
  331.         }
  332.         else
  333.         if(!$this->validate_email($_POST['email']))
  334.         {
  335.             $this->add_error("Please provide a valid email address");
  336.             $ret = false;
  337.         }

  338.         //message validaions
  339.         if(strlen($_POST['message'])>2048)
  340.         {
  341.             $this->add_error("Message is too big!");
  342.             $ret = false;
  343.         }

  344.         //captcha validaions
  345.         if(isset($this->captcha_handler))
  346.         {
  347.             if(!$this->captcha_handler->Validate())
  348.             {
  349.                 $this->add_error($this->captcha_handler->GetError());
  350.                 $ret = false;
  351.             }
  352.         }
  353.         //file upload validations
  354.         if(!empty($this->fileupload_fields))
  355.         {
  356.          if(!$this->ValidateFileUploads())
  357.          {
  358.             $ret = false;
  359.          }
  360.         }
  361.         return $ret;
  362.     }

  363.     function ValidateFileType($field_name,$valid_filetypes)
  364.     {
  365.         $ret=true;
  366.         $info = pathinfo($_FILES[$field_name]['name']);
  367.         $extn = $info['extension'];
  368.         $extn = strtolower($extn);

  369.         $arr_valid_filetypes= explode(',',$valid_filetypes);
  370.         if(!in_array($extn,$arr_valid_filetypes))
  371.         {
  372.             $this->add_error("Valid file types are: $valid_filetypes");
  373.             $ret=false;
  374.         }
  375.         return $ret;
  376.     }

  377.     function ValidateFileSize($field_name,$max_size)
  378.     {
  379.         $size_of_uploaded_file =
  380.                 $_FILES[$field_name]["size"]/1024;//size in KBs
  381.         if($size_of_uploaded_file > $max_size)
  382.         {
  383.             $this->add_error("The file is too big. File size should be less than $max_size KB");
  384.             return false;
  385.         }
  386.         return true;
  387.     }

  388.     function IsFileUploaded($field_name)
  389.     {
  390.         if(empty($_FILES[$field_name]['name']))
  391.         {
  392.             return false;
  393.         }
  394.         if(!is_uploaded_file($_FILES[$field_name]['tmp_name']))
  395.         {
  396.             return false;
  397.         }
  398.         return true;
  399.     }
  400.     function ValidateFileUploads()
  401.     {
  402.         $ret=true;
  403.         foreach($this->fileupload_fields as $upld_field)
  404.         {
  405.             $field_name = $upld_field["name"];

  406.             $valid_filetypes = $upld_field["file_types"];
  407.             
  408.             if(!$this->IsFileUploaded($field_name))
  409.             {
  410.                 continue;
  411.             }

  412.             if($_FILES[$field_name]["error"] != 0)
  413.             {
  414.                 $this->add_error("Error in file upload; Error code:".$_FILES[$field_name]["error"]);
  415.                 $ret=false;
  416.             }

  417.             if(!empty($valid_filetypes) &&
  418.              !$this->ValidateFileType($field_name,$valid_filetypes))
  419.             {
  420.                 $ret=false;
  421.             }

  422.             if(!empty($upld_field["maxsize"]) &&
  423.             $upld_field["maxsize"]>0)
  424.             {
  425.                 if(!$this->ValidateFileSize($field_name,$upld_field["maxsize"]))
  426.                 {
  427.                     $ret=false;
  428.                 }
  429.             }

  430.         }
  431.         return $ret;
  432.     }

  433.     function StripSlashes($str)
  434.     {
  435.         if(get_magic_quotes_gpc())
  436.         {
  437.             $str = stripslashes($str);
  438.         }
  439.         return $str;
  440.     }
  441.     /*
  442.     Sanitize() function removes any potential threat from the
  443.     data submitted. Prevents email injections or any other hacker attempts.
  444.     if $remove_nl is true, newline chracters are removed from the input.
  445.     */
  446.     function Sanitize($str,$remove_nl=true)
  447.     {
  448.         $str = $this->StripSlashes($str);

  449.         if($remove_nl)
  450.         {
  451.             $injections = array('/(\n+)/i',
  452.                 '/(\r+)/i',
  453.                 '/(\t+)/i',
  454.                 '/(%0A+)/i',
  455.                 '/(%0D+)/i',
  456.                 '/(%08+)/i',
  457.                 '/(%09+)/i'
  458.                 );
  459.             $str = preg_replace($injections,'',$str);
  460.         }

  461.         return $str;
  462.     }

  463.     /*Collects clean data from the $_POST array and keeps in internal variables.*/
  464.     function CollectData()
  465.     {
  466.         $this->name = $this->Sanitize($_POST['name']);
  467.         $this->email = $this->Sanitize($_POST['email']);

  468.         /*newline is OK in the message.*/
  469.         $this->message = $this->StripSlashes($_POST['message']);
  470.     }

  471.     function add_error($error)
  472.     {
  473.         array_push($this->errors,$error);
  474.     }
  475.     function validate_email($email)
  476.     {
  477.         return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}[        DISCUZ_CODE_1        ]quot;, $email);
  478.     }

  479.     function GetKey()
  480.     {
  481.         return $this->form_random_key.$_SERVER['SERVER_NAME'].$_SERVER['REMOTE_ADDR'];
  482.     }

  483. }

  484. ?>
复制代码



回复

使用道具 举报

发表于 27-9-2015 02:08 PM | 显示全部楼层
chew2293 发表于 27-9-2015 03:20 AM
这是form

还有这个是有连接form的,从提交资料到email都有关系的,不懂有没有关系,不过也分享出来吧

阿彌陀佛
大大偶速奶亂滴……
做墨鬼你抄經念佛變成奶寫 html 鳥啊……

參考 #2 和 #3 樓大大的方案:
第一個 form 45 行那裏
  1. <form id='contactus' action='<?php echo $formproc->GetSelfScript(); ?>' method='post' enctype="multipart/form-data" accept-charset='UTF-8'>
复制代码

要加東西變成

<form id='contactus' action='<?php echo $formproc->GetSelfScript(); ?>' method='post' enctype="multipart/form-data" accept-charset='UTF-8' onsubmit=function()>

onsubmit=function()

可以用直接 => #3 樓大大的方式

間接 => #2 樓大大的方式

或如果要 #4 樓大大的方式,第 40 行之後要加
  1. <script src="jquery-1.11.3.min.js"></script>
复制代码


回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 27-9-2015 02:54 PM | 显示全部楼层
netizen 发表于 27-9-2015 02:08 PM
阿彌陀佛
大大偶速奶亂滴……
做墨鬼你抄經念佛變成奶寫 html 鳥啊……

阿弥陀佛 _/\_ 学佛和工作也是可以在一起的
不过我的工作是杂杂的,没有读过,都是自学,
看了你的回复,感觉有点深,我再消化消化下,
不然你帮我加上去后,然后我在参考是哪里的问题,
因为有些代码我不熟,如果你肯帮我写上去后,然后我再参考那会更好
回复

使用道具 举报

 楼主| 发表于 27-9-2015 02:57 PM | 显示全部楼层
netizen 发表于 27-9-2015 02:08 PM
阿彌陀佛
大大偶速奶亂滴……
做墨鬼你抄經念佛變成奶寫 html 鳥啊……

大概get到了,我试试了在给你知道

评分

参与人数 1人气 +5 收起 理由
netizen + 5 祝六時吉祥,法喜充滿 :)

查看全部评分

回复

使用道具 举报

发表于 27-9-2015 03:00 PM | 显示全部楼层
chew2293 发表于 27-9-2015 02:54 PM
阿弥陀佛 _/\_ 学佛和工作也是可以在一起的
不过我的工作是杂杂的,没有读过,都是自学,
看了你的回 ...

大大從事的行業非常如法,符合釋尊說的正命,阿彌陀佛
偶開大大玩笑切勿介意啊

大大的 code 東拼西凑啊~
那個 PHP 加 Javascript 加 HTML……
很難搞的說
畢竟不知道大大測試的 infra 是哪一個
所以衹好用個大略的 onsubmit=function()


回复

使用道具 举报

 楼主| 发表于 27-9-2015 03:09 PM | 显示全部楼层
netizen 发表于 27-9-2015 02:08 PM
阿彌陀佛
大大偶速奶亂滴……
做墨鬼你抄經念佛變成奶寫 html 鳥啊……

1.PNG

2.PNG

还是不可以,是不是submit罢了我排位放错了?

回复

使用道具 举报

 楼主| 发表于 27-9-2015 03:18 PM | 显示全部楼层
netizen 发表于 27-9-2015 03:00 PM
大大從事的行業非常如法,符合釋尊說的正命,阿彌陀佛
偶開大大玩笑切勿介意啊

第4楼的我不懂要怎样实现在form那里,老实说,我不会自己写的,我只会修改和加一些基本的美观,和基本的html,太深我真的不会
所以希望你们不要骂我,这个form我也是拿来当做学习学习

回复

使用道具 举报

发表于 27-9-2015 03:19 PM | 显示全部楼层
chew2293 发表于 27-9-2015 03:09 PM
还是不可以,是不是submit罢了我排位放错了?

暈了……

大大試試看這個先~
可以才再 mod 吧~

第一個 form 45 行那裏用:
  1. <form id='contactus' action='<?php echo $formproc->GetSelfScript(); ?>' method='post' onsubmit="return confirm('代入符合所要求 confirmation box 之内容');" enctype="multipart/form-data" accept-charset='UTF-8'>
复制代码


如果可以 run,
蛋速想要比較複雜 look 的
就第一個 form 45 行那裏用:
  1. <form id='contactus' action='<?php echo $formproc->GetSelfScript(); ?>' method='post' onsubmit="onSubmit(event) enctype="multipart/form-data" accept-charset='UTF-8'>
复制代码


然後 85 行之後加入
  1. <script>
  2.         function onSubmit(e) {            
  3.             if (!confirm("代入符合所要求 confirmation box 之内容")) {
  4.                 e.preventDefault();  //阻止事件进行
  5.             }
  6.         }
  7.     </script>
复制代码
回复

使用道具 举报

发表于 27-9-2015 03:22 PM | 显示全部楼层
chew2293 发表于 27-9-2015 03:18 PM
第4楼的我不懂要怎样实现在form那里,老实说,我不会自己写的,我只会修改和加一些基本的美观,和基本的h ...

偶不速南先生 / 李宗吾 / 李敖
偶不費罵你滴~

大大有心學速好事奶滴~
偶醬故意講外星話馬不速一樣討罵~
中罵修忍辱好用啊~
回复

使用道具 举报

 楼主| 发表于 27-9-2015 03:47 PM | 显示全部楼层
netizen 发表于 27-9-2015 03:22 PM
偶不速南先生 / 李宗吾 / 李敖
偶不費罵你滴~

你回复的字要我用另外一种方式来阅读才明白
上面的方法,我现在试试,有不明白再问问

回复

使用道具 举报


ADVERTISEMENT

发表于 27-9-2015 03:52 PM | 显示全部楼层
chew2293 发表于 27-9-2015 03:47 PM
你回复的字要我用另外一种方式来阅读才明白
上面的方法,我现在试试,有不明白再问问

宗門教下禪宗不用言語的~
偶用外星語速門外汗奶滴~
大大度得明白就木有罵偶~
其他度不明白直接不鳥偶~

偶有素外出一陣子,如未即時回復且見諒啊~
回复

使用道具 举报

 楼主| 发表于 27-9-2015 04:05 PM | 显示全部楼层
netizen 发表于 27-9-2015 03:19 PM
暈了……

大大試試看這個先~
  1. <?PHP
  2. /*
  3.     Contact Form from HTML Form Guide
  4.     This program is free software published under the
  5.     terms of the GNU Lesser General Public License.
  6.     See this page for more info:
  7.     http://www.html-form-guide.com/contact-form/contact-form-attachment.html
  8. */
  9. require_once("./include/fgcontactform.php");

  10. $formproc = new FGContactForm();

  11. //1. Add your email address here.
  12. //You can add more than one receipients.
  13. $formproc->AddRecipient('tests@html-form-guide.com'); //<<---Put your email address here


  14. //2. For better security. Get a random tring from this link: http://tinyurl.com/randstr
  15. // and put it here
  16. $formproc->SetFormRandomKey('HG9hPBpn9Bn26yg');

  17. $formproc->AddFileUploadField('photo','jpg,jpeg,gif,png,bmp',2024);

  18. if(isset($_POST['submitted']))
  19. {
  20.    if($formproc->ProcessForm())
  21.    {
  22.         $formproc->RedirectToURL("thank-you.php");
  23.    }
  24. }

  25. ?>
  26. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  27. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
  28. <head>
  29.       <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
  30.       <title>Contact us</title>
  31.       <link rel="STYLESHEET" type="text/css" href="contact.css" />
  32.       <script type='text/javascript' src='scripts/gen_validatorv31.js'></script>
  33.       <script type='text/javascript' src='scripts/fg_captcha_validator.js'></script>
  34. </head>
  35. <body>

  36. <!-- Form Code Start -->
  37. <form id='contactus' action='<?php echo $formproc->GetSelfScript(); ?>' method='post' onsubmit="return confirm('代入符合所要求 confirmation box 之内容');" enctype="multipart/form-data" accept-charset='UTF-8'>

  38. <fieldset >
  39. <legend>Contact us</legend>

  40. <input type='hidden' name='submitted' id='submitted' value='1'/>
  41. <input type='hidden' name='<?php echo $formproc->GetFormIDInputName(); ?>' value='<?php echo $formproc->GetFormIDInputValue(); ?>'/>
  42. <input type='text'  class='spmhidip' name='<?php echo $formproc->GetSpamTrapInputName(); ?>' />

  43. <div class='short_explanation'>* required fields</div>

  44. <div><span class='error'><?php echo $formproc->GetErrorMessage(); ?></span></div>
  45. <div class='container'>
  46.     <label for='name' >Your Full Name*: </label><br/>
  47.     <input type='text' name='name' id='name' value='<?php echo $formproc->SafeDisplay('name') ?>' maxlength="50" /><br/>
  48.     <span id='contactus_name_errorloc' class='error'></span>
  49. </div>
  50. <div class='container'>
  51.     <label for='email' >Email Address*:</label><br/>
  52.     <input type='text' name='email' id='email' value='<?php echo $formproc->SafeDisplay('email') ?>' maxlength="50" /><br/>
  53.     <span id='contactus_email_errorloc' class='error'></span>
  54. </div>
  55. <div class='container'>
  56.     <label for='message' >Message:</label><br/>
  57.     <span id='contactus_message_errorloc' class='error'></span>
  58.     <textarea rows="10" cols="50" name='message' id='message'><?php echo $formproc->SafeDisplay('message') ?></textarea>
  59. </div>
  60. <div class='container'>
  61.     <label for='photo' >Upload your photo:</label><br/>
  62.     <input type="file" name='photo' id='photo' /><br/>
  63.     <span id='contactus_photo_errorloc' class='error'></span>
  64. </div>


  65. <div class='container'>
  66.     <input type='submit' name='Submit' value='Submit' />
  67. </div>

  68. </fieldset>
  69. </form>
  70.     <script>
  71.             function onSubmit(e) {            
  72.                 if (!confirm("代入符合所要求 confirmation box 之内容")) {
  73.                     e.preventDefault();  //阻止事件进行
  74.                 }
  75.             }
  76.         </script>
  77. <!-- client-side Form Validations:
  78. Uses the excellent form validation script from JavaScript-coder.com-->

  79. <script type='text/javascript'>
  80. // <![CDATA[

  81.     var frmvalidator  = new Validator("contactus");
  82.     frmvalidator.EnableOnPageErrorDisplay();
  83.     frmvalidator.EnableMsgsTogether();
  84.     frmvalidator.addValidation("name","req","Please provide your name");

  85.     frmvalidator.addValidation("email","req","Please provide your email address");

  86.     frmvalidator.addValidation("email","email","Please provide a valid email address");

  87.     frmvalidator.addValidation("message","maxlen=2048","The message is too long!(more than 2KB!)");

  88.     frmvalidator.addValidation("photo","file_extn=jpg;jpeg;gif;png;bmp","Upload images only. Supported file types are: jpg,gif,png,bmp");
  89. // ]]>
  90. </script>
  91. </body>
  92. </html>
复制代码
照你方式了,还是不行,是不是哪里有错??
还是你帮我直接加,然后我自己在用beyond compare哪里出错就行了。

回复

使用道具 举报

发表于 27-9-2015 04:16 PM | 显示全部楼层
chew2293 发表于 27-9-2015 04:05 PM
照你方式了,还是不行,是不是哪里有错??
还是你帮我直接加,然后我自己在用beyond compare哪里出错就 ...

大大…… 二選一~ 二選一啊~ 表那麽貪心好不好

速不速辣過一比二多滴廣告太洗腦鳥

  1. <?PHP
  2. /*
  3.     Contact Form from HTML Form Guide
  4.     This program is free software published under the
  5.     terms of the GNU Lesser General Public License.
  6.     See this page for more info:
  7.     http://www.html-form-guide.com/contact-form/contact-form-attachment.html
  8. */
  9. require_once("./include/fgcontactform.php");

  10. $formproc = new FGContactForm();

  11. //1. Add your email address here.
  12. //You can add more than one receipients.
  13. $formproc->AddRecipient('tests@html-form-guide.com'); //<<---Put your email address here


  14. //2. For better security. Get a random tring from this link: http://tinyurl.com/randstr
  15. // and put it here
  16. $formproc->SetFormRandomKey('HG9hPBpn9Bn26yg');

  17. $formproc->AddFileUploadField('photo','jpg,jpeg,gif,png,bmp',2024);

  18. if(isset($_POST['submitted']))
  19. {
  20.    if($formproc->ProcessForm())
  21.    {
  22.         $formproc->RedirectToURL("thank-you.php");
  23.    }
  24. }

  25. ?>
  26. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  27. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
  28. <head>
  29.       <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
  30.       <title>Contact us</title>
  31.       <link rel="STYLESHEET" type="text/css" href="contact.css" />
  32.       <script type='text/javascript' src='scripts/gen_validatorv31.js'></script>
  33.       <script type='text/javascript' src='scripts/fg_captcha_validator.js'></script>
  34. </head>
  35. <body>

  36. <!-- Form Code Start -->
  37. <form id='contactus' action='<?php echo $formproc->GetSelfScript(); ?>' method='post' onsubmit="return confirm('confirm submit?');" enctype="multipart/form-data" accept-charset='UTF-8'>

  38. <fieldset >
  39. <legend>Contact us</legend>

  40. <input type='hidden' name='submitted' id='submitted' value='1'/>
  41. <input type='hidden' name='<?php echo $formproc->GetFormIDInputName(); ?>' value='<?php echo $formproc->GetFormIDInputValue(); ?>'/>
  42. <input type='text'  class='spmhidip' name='<?php echo $formproc->GetSpamTrapInputName(); ?>' />

  43. <div class='short_explanation'>* required fields</div>

  44. <div><span class='error'><?php echo $formproc->GetErrorMessage(); ?></span></div>
  45. <div class='container'>
  46.     <label for='name' >Your Full Name*: </label><br/>
  47.     <input type='text' name='name' id='name' value='<?php echo $formproc->SafeDisplay('name') ?>' maxlength="50" /><br/>
  48.     <span id='contactus_name_errorloc' class='error'></span>
  49. </div>
  50. <div class='container'>
  51.     <label for='email' >Email Address*:</label><br/>
  52.     <input type='text' name='email' id='email' value='<?php echo $formproc->SafeDisplay('email') ?>' maxlength="50" /><br/>
  53.     <span id='contactus_email_errorloc' class='error'></span>
  54. </div>
  55. <div class='container'>
  56.     <label for='message' >Message:</label><br/>
  57.     <span id='contactus_message_errorloc' class='error'></span>
  58.     <textarea rows="10" cols="50" name='message' id='message'><?php echo $formproc->SafeDisplay('message') ?></textarea>
  59. </div>
  60. <div class='container'>
  61.     <label for='photo' >Upload your photo:</label><br/>
  62.     <input type="file" name='photo' id='photo' /><br/>
  63.     <span id='contactus_photo_errorloc' class='error'></span>
  64. </div>


  65. <div class='container'>
  66.     <input type='submit' name='Submit' value='Submit' />
  67. </div>

  68. </fieldset>
  69. </form>
  70. <!-- client-side Form Validations:
  71. Uses the excellent form validation script from JavaScript-coder.com-->

  72. <script type='text/javascript'>
  73. // <![CDATA[

  74.     var frmvalidator  = new Validator("contactus");
  75.     frmvalidator.EnableOnPageErrorDisplay();
  76.     frmvalidator.EnableMsgsTogether();
  77.     frmvalidator.addValidation("name","req","Please provide your name");

  78.     frmvalidator.addValidation("email","req","Please provide your email address");

  79.     frmvalidator.addValidation("email","email","Please provide a valid email address");

  80.     frmvalidator.addValidation("message","maxlen=2048","The message is too long!(more than 2KB!)");

  81.     frmvalidator.addValidation("photo","file_extn=jpg;jpeg;gif;png;bmp","Upload images only. Supported file types are: jpg,gif,png,bmp");
  82. // ]]>
  83. </script>
  84. </body>
  85. </html>
复制代码


偶敗倒……
大大繞了偶啊……
回复

使用道具 举报

 楼主| 发表于 27-9-2015 04:32 PM | 显示全部楼层
netizen 发表于 27-9-2015 04:16 PM
大大…… 二選一~ 二選一啊~ 表那麽貪心好不好

速不速辣過一比二多滴廣告太洗腦鳥

我试了你的code..我整份copy了,还是不可以
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 7-8-2025 09:22 PM , Processed in 0.222975 second(s), 34 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表