简体   繁体   English

PHP-onClick似乎不起作用

[英]PHP - onClick doesn't seem to be working

I have series of dropdown boxes which can be selected by the user. 我有一系列下拉框,用户可以选择。 I then want to have two different submit buttons that will do two different actions with the selected data. 然后,我希望有两个不同的提交按钮,这些按钮将对所选数据执行两个不同的操作。 I am trying to code the submit button to run the selected php, but when I click the button does nothing. 我正在尝试对提交按钮进行编码以运行所选的php,但是当我单击按钮时却什么也没做。 Any help is appreciated, my code is below. 任何帮助表示赞赏,我的代码如下。 AFAIK the only relevant bits are my formSubmit function and the input tag near the bottom of the code. AFAIK唯一相关的位是我的formSubmit函数和代码底部附近的输入标签。

edit: I have edited out a bulk of the code and left the pieces that I think are relevant. 编辑:我已编辑掉大部分代码,并保留了我认为相关的部分。 Please let me know if more info is needed. 请让我知道是否需要更多信息。

<!DOCTYPE html>
<?php
require 'config.php';  // Database connection
//////// End of connecting to database ////////
?>
<html>
<head>
<SCRIPT language="JavaScript">
//Scripts

function submitForm(action)
    {
        document.getElementById('f1').action = action;
        document.getElementById('f1').submit();
    }

</script>
</head>
<body>
<div>   
<?Php    

//Beginning of Form
echo "<form method=post name='f1' action =''>";
//Dropdown boxes are here


//This line is what is not working:
echo "<input type='submit' value='Careers' onclick=\"submitForm('rt.php')\">";
echo "</form>";
?>
</div>
</body>
</html>

Not quite sure what is wrong with the function, xe4me may have the correct answer. 不太确定函数有什么问题,xe4me可能有正确的答案。 However, I just changed my onClick to this and it worked: 但是,我只是将onClick更改为此,并且它起作用了:

onClick=\"document.f1.action='rt.php'; document.f1.submit(); return true;\"

You're using document.getElementById('f1') in submitForm function 您正在submitForm函数中使用document.getElementById('f1')

function submitForm(action)
{
    document.getElementById('f1').action = action;
    document.getElementById('f1').submit();
}

But your form doesn't have id attribute because of this line of code 但是由于这一行代码,您的表单没有id属性

echo "<form method=post name='f1' action =''>";

so the form won't be submitted when you click the Careers button. 因此,当您点击“ Careers按钮时,该表单将不会提交。 You need to add id='f1' attribute to the form by changing the above line of code to below 您需要通过将上面的代码行更改为下面的代码来向表单添加id='f1'属性

echo "<form id='f1' method='post' name='f1' action =''>";

Try the jQuery method : 试试jQuery方法:

http://api.jquery.com/jquery.post/ with How to get ID of clicked element with jQuery http://api.jquery.com/jquery.post/以及如何使用jQuery获取被单击元素的ID

Whenever any button is clicked, find its id and based on that, use $.post to post data to the location you want. 每当单击任何按钮时,都要找到其ID,然后根据其ID使用$ .post将数据发布到所需的位置。

you must add a class to your button and call the onclick based on that class name : 您必须在按钮上添加一个类,然后根据该类名称调用onclick:

 <?Php    

  ....

 echo "<input type='submit' class='submitter' data-action='rt.php' value='Careers' >";
 echo "</form>";
 ?>

// In Javascript : //在Javascript中:

 document.getElementByClassName('submitter').onclick = function(){
  var action  = this.data('action');

   document.getElementById('f1').action = action;
   document.getElementById('f1').submit();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM