如何在选择框中的值发生变化时使用 AJAX、JSON 和 PHP 检索表单?

发布于 2024-10-08 10:03:49 字数 2108 浏览 2 评论 0原文

我有一个选择框,在选择其中的值时,我必须显示一个表单,其中有一个包含 javascript 日历功能的日期字段。我尝试使用普通的 AJAX 和 PHP 组合,但我没有在其中获取日历,所以我只需要知道如何使用 JSON 和 AJAX 和 PHP 实现它?

感谢

每一个帮助......

代码

这是我正在使用AJAX功能的Javascript:

<script>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function dochange(path,val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
 if (req.readyState==4) {
      if (req.status==200) {
            document.getElementById('docfields').innerHTML="";
            if(req.responseText != ''){
           document.getElementById('docfields').innerHTML=req.responseText; //retuen value
            }else{
            document.getElementById('docfields').innerHTML="<br>&nbsp;\t<font size='2'><b>No Fields Available</b></font>";
            }
      }
 }
};
req.open("GET", path+"getDocFields.php?doctype_id="+val); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}
</script>

下面是我需要用脚本标签中实例化的日历对象(tcal)来显示的HTML表单:

<form onsubmit="" action="" method="post" name="newdoc">
         <table border="0" style="border: medium none;" id="docfield">
          <tbody>
          <tr>
            <td style="border-right: medium none;">
                Date
            </td>
         <td style="border-right: medium none;">
            <input type="text" value="" maxlength="10" name="Test" style="width: 100px;" id="date">
        <script>
         new tcal ({  
      'formname': 'newdoc',  
      'controlname': 'Test'
         });
     </script>
        </td>
     </tr>
    </tbody>
 </table>
</form>

我没有显示日历...

I Have a select box, on selectinga value in it I have to display a form in which there is a Date field which include a javascript calendar functionalty. I tried it with normal AJAX and PHP combination, but I dont get the Calendar in it, So I just need to know How I can make it happen using JSON and AJAX and PHP?

Thanks

Every Help Is Appreciated....

THE CODE

This is The Javascript I am Using to AJAX function:

<script>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function dochange(path,val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
 if (req.readyState==4) {
      if (req.status==200) {
            document.getElementById('docfields').innerHTML="";
            if(req.responseText != ''){
           document.getElementById('docfields').innerHTML=req.responseText; //retuen value
            }else{
            document.getElementById('docfields').innerHTML="<br> \t<font size='2'><b>No Fields Available</b></font>";
            }
      }
 }
};
req.open("GET", path+"getDocFields.php?doctype_id="+val); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}
</script>

Below Is the HTML form which I need to display with Calendar object (tcal) instantiated in the script tag:

<form onsubmit="" action="" method="post" name="newdoc">
         <table border="0" style="border: medium none;" id="docfield">
          <tbody>
          <tr>
            <td style="border-right: medium none;">
                Date
            </td>
         <td style="border-right: medium none;">
            <input type="text" value="" maxlength="10" name="Test" style="width: 100px;" id="date">
        <script>
         new tcal ({  
      'formname': 'newdoc',  
      'controlname': 'Test'
         });
     </script>
        </td>
     </tr>
    </tbody>
 </table>
</form>

I am not getting the calendar displayed...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾挽 2024-10-15 10:03:49

我在响应 html 中看到 javascrip,如果 html 通过 ajax 调用,则永远不会执行该 javascrip。
尝试把那个javascript

new tcal ({
'formname': 'newdoc',
'controlname': 'Test'
});

after your XMLHttpRequest object fetches and puts data in '#docfields'.

if (req.readyState==4)
{
   if (req.status==200)
   {
      document.getElementById('docfields').innerHTML="";
      if(req.responseText != '')
      {
          document.getElementById('docfields').innerHTML=req.responseText; //retuen value
          /** here **/
          new tcal ({'formname': 'newdoc','controlname': 'Test'});
          /** here **/
      }
      else
      {
          document.getElementById('docfields').innerHTML="No Fields Available";
      }
   }
}

希望它会有所帮助。

I see javascrip in the response html, which will never be executed if the html is coming through ajax call.
Try to put that javascript

new tcal ({
'formname': 'newdoc',
'controlname': 'Test'
});

after your XMLHttpRequest object fetches and puts data in '#docfields'.

if (req.readyState==4)
{
   if (req.status==200)
   {
      document.getElementById('docfields').innerHTML="";
      if(req.responseText != '')
      {
          document.getElementById('docfields').innerHTML=req.responseText; //retuen value
          /** here **/
          new tcal ({'formname': 'newdoc','controlname': 'Test'});
          /** here **/
      }
      else
      {
          document.getElementById('docfields').innerHTML="No Fields Available";
      }
   }
}

Hope it will help.

思慕 2024-10-15 10:03:49

伊姆兰是对的。您需要提取脚本代码并运行它。
这段代码可以很常用:

var scripts = "";
$('form)'.find("script").each(function() {
            var content = $(this).html();
            content = content.replace(/<!--/g, "").replace(/(\/\/)?-->/g, "");
            scripts += content;
        });
eval (scripts); // eval is evil, but in this case...

Imran is right. You need to extract the scriptcode and run it.
This code can be used very commonly:

var scripts = "";
$('form)'.find("script").each(function() {
            var content = $(this).html();
            content = content.replace(/<!--/g, "").replace(/(\/\/)?-->/g, "");
            scripts += content;
        });
eval (scripts); // eval is evil, but in this case...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文