为什么函数中的变量不传递给HTML文章?

时间:2019-07-28 05:24:51

标签: javascript html forms article

我可以获取变量以作为警报显示在函数中,但是我无法获取该变量以传递给文章。我在做什么错了?

我知道“ a”保存了正确的信息,因为我已将其显示为带有警报。

我尝试使用以下方法传递变量:placeholderContent.recipientName = a;

我尝试在函数的内部和外部声明变量,但这也不起作用。

我尝试制作一个嵌套函数来传递变量。

我是javascript的新手,已经花了数小时研究它并尝试使用不同的方法,但我无法使用它。我希望有人能帮助我!

 function validateForm() {           
                a = document.forms["inviteForm"]["recipientName"].value;
                if (a == "") {
                    alert("Name must be filled out.");
                return false;
                         }
                document.getElementById("recipientName").textContent = a;
                }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="pageForm">
            <form action="#" Name=inviteForm onsubmit="return validateForm()">
                <label for="recipientName">Recipient name:</label>
                <input type="text" name="recipientName" id="recipientName" placeholder="Enter your Recipient Name" />
                <input type="submit" value="submit" />
            </form>
        </section>

        <article id="placeholderContent">
            Hello <span id="recipientName"></span>!
        </article>

在用户在表单中输入名称并单击提交按钮之后,应该在页面上显示变量receiveName。

2 个答案:

答案 0 :(得分:1)

您需要做两件事:

  1. validateForm()函数末尾返回span,以使页面不会刷新
  2. 更改要在其中显示数据的input元素的ID。当前,该ID与<!DOCTYPE html> <html lang="en-US"> <head> <title>Invite Page</title> <link rel="stylesheet" type="text/css" href="css/main.css" /> </head> <body> <section id="pageForm"> <form action="#" Name=inviteForm onsubmit="return validateForm()"> <label for="recipientName">Recipient name:</label> <input type="text" name="recipientName" id="recipientName" placeholder="Enter your Recipient Name" /> <input type="submit" value="submit" /> </form> </section> <article id="placeholderContent"> Hello <span id="recipientNameDisplay"></span>! </article> <script> function validateForm() { a = document.forms["inviteForm"]["recipientName"].value; if (a == "") { alert("Name must be filled out."); return false; } document.getElementById("recipientNameDisplay").textContent = a; return false; } </script> </body> </html> 元素的ID冲突。

这是更新的代码:

 $main_url="http://programming.com";
 $str = file_get_contents($main_url);

 // Gets Webpage Title
 if(strlen($str)>0)
 {

      $str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>
      preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
      $title=$title[1];
 }

 // Gets Webpage Description
 $b =$main_url;
 @$url = parse_url( $b );
 @$tags = get_meta_tags($url['scheme'].'://'.$url['host'] );
 $description=$tags['description'];

 // Gets Webpage Internal Links
 $doc = new DOMDocument; 
 @$doc->loadHTML($str); 

 $items = $doc->getElementsByTagName('a'); 
 foreach($items as $value) 
 { 
      $attrs = $value->attributes; 

      $sec_url[]=$attrs->getNamedItem('href')->nodeValue;

 }

 /*foreach ($sec_url as  $value) {
        print_r($value);

        ?>
    <br>
        <?php

}*/

foreach($sec_url as $value)
{

    $sq2 = "insert into datascience (link,title,description,internal_link) 

                     values('$main_url','$title','$description','$value')";  

    $res= mysqli_query($conn, $sq2);

答案 1 :(得分:0)

Id是唯一的,并且您在recipientNamearticle字段中都使用了input,因此无法在同一页面中使用两次。我更改了文章中的ID,一切正常。

 function validateForm() {           
            a = document.forms["inviteForm"]["recipientName"].value;
            if (a == "") {
                alert("Name must be filled out.");
            return false;
                     }
            document.getElementById("recipientName1").textContent = a;
            }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="pageForm">
        <form action="#" Name=inviteForm onsubmit="return validateForm()">
            <label for="recipientName">Recipient name:</label>
            <input type="text" name="recipientName" id="recipientName" placeholder="Enter your Recipient Name" />
            <input type="submit" value="submit" />
        </form>
    </section>

    <article id="placeholderContent">
        Hello <span id="recipientName1"></span>!
    </article>