English 中文(简体)
POST 到 PHP 从 Javascript 到 PHP
原标题:POST to PHP from javascript

目前我有一个 HTML 页面, 有许多文本框, 需要通过 Javascript 验证。 一旦它们全部被验证, 我想通过控制器创建用户账户 。 php

我的问题是: 我怎样才能从 Javascript 函数中将 PHP 文件放在 PHP 文件上 。 我认为我可以使用 AJax, 但是如果可能的话, 我想不用它。

感谢 谢谢

最佳回答

创建带有日志方法的窗体, 并将其指向php 脚本。 如果您在下面设置了这样的窗体, 您不需要从 javascript 函数中发布, 因为 html 窗体的默认行为将会为您做发布 。 对于没有启用 javascript 的人来说, 它也会优雅地降低 。

<form action="/path.to.your.php.file.php" method="POST" onSubmit="return validate();">
 ... your fields...
 <input type="submit" value="validate and submit"/>
</form>

<script>
  function validate(){
    // do your validation here
    return true; // return true if validation was ok, false otherwise
  }
</script>
问题回答

如果您想要使用 AJAAX,请使用 JQuery 库,您可以做以下工作将

$.ajax({
   type:  POST ,
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

$.post( controller.php , function(data) {
    //do something with the result
});

指定您表格的名称, 像这样...

<form name="myForm" id="form1" method="post" action="phpScriptToPostTo.php">

...然后运行这个喷射器:

document.forms["myForm"].submit();

...或者这个在jQuery:

$( #form1 ).submit();

...或这个在jQuery的ajax:

$.post( server.php , $( #form1 ).serialize())




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签