简体   繁体   English

提交按钮单击将其值更改为另一件事

[英]Submit button clicking changing the value of it to another thing

maybe very easy!也许很容易! I'm php coder and I don't have experience in js but I must do this for one of my codes suppose I have sub1 in page after clicking it must be that sub1 but value now is sub2我是 php 编码器,我没有 js 经验,但我必须为我的一个代码执行此操作,假设我在点击后页面中有 sub1,它必须是 sub1,但现在的值是 sub2

       <html>
        <head>
        <title>pharmacy</title>
        </head>
        <body>
        <form method="post" action="pharmacy.php">
        <?php
       //some code
            if(array_key_exists('update',$_POST)){
                //somecode
                }
        ?>
<input type="submit" name="update" value="<?php echo if(isset($_GET['update'])) ? 'Show' : 'Update' ?> ">
    </form>
    </body>
    </html>

show as function name does not really make sense here (imo), but you could do: show为 function 名称在这里没有意义(imo),但你可以这样做:

<input type="submit" name="sub" value="sub1" onclick="show(this)">

and

function show(element) {
    element.value = 'sub2';
}

Important:重要的:

But that will actually not solve your problem.但这实际上并不能解决您的问题。 As soon as you click the button, the form is submitted, meaning the browser initiates a new request and will load a new page .一旦你点击按钮,表单就会被提交,这意味着浏览器会发起一个新的请求并加载一个新的页面 So every change you made the current page is lost anyway.因此,您对当前页面所做的每项更改都会丢失。

The question is: What are you trying to do?问题是:你想做什么?

It seems to me that you should change the value of the button on the server side.在我看来,您应该更改服务器端按钮的值。 You have to keep track which form was submitted (or how often, I don't know what you are trying to do) and set the value of the button accordingly.您必须跟踪提交的表单(或者多久提交一次,我不知道您要做什么)并相应地设置按钮的值。

Update:更新:

I see several possibilities to solve this:我看到了解决这个问题的几种可能性:

  1. You could keep using JavaScript and send and get the data via Ajax .您可以继续使用 JavaScript 并通过Ajax发送和获取数据。 As you have no experience with JavaScript, I would say you have to learn more about JavaScript and Ajax first before you can use it.由于您没有使用 JavaScript 的经验,我想说您必须先了解有关 JavaScript 和 Ajax 的更多信息,然后才能使用它。

  2. You could add a GET parameter in your URL with which you can know which label to show for the button.您可以在 URL 中添加一个 GET 参数,您可以通过该参数知道要为按钮显示哪个 label。 Example:例子:

     <form method="post" action="?update=1">

    and

    <input type="submit" name="sub" value="<?php echo isset($_GET['update'])? 'Show': 'Update'?> ">
  3. Similar to 2, but use a session variable (and not a GET parameter) to keep track of the state.类似于 2,但使用session 变量(而不是 GET 参数)来跟踪 state。

Update2:更新2:

As you are already having $_POST['update'] you don't need the URL parameter.由于您已经拥有$_POST['update'] ,因此您不需要 URL 参数。 It could just be:它可能只是:

<html>
    <head>
        <title>pharmacy</title>
    </head>
    <body>
        <form method="post" action="pharmacy.php">
        <input type="submit" name="update" value="<?php echo isset($_POST['update']) ? 'Update' : 'Show'; ?> ">
        </form>
    </body>
</html>
<html>
<head>
<title>test</title>
<script>
function show()
{
    document.getElementById("sub").value= "sub2";
            return true;
}
</script>
</head>
<body>

<form method="post">
<input type='submit' id="sub" name='sub' value="sub1" onclick="return show()">
</form>
</body>
</html>

This should do it这应该这样做

        function show(){
        document.getElementsByName('sub')[0].value = 'sub2';
        return false;
        }

Edit: if you don't want it to submit the form, just add a return false , but then you'd need to change your onclick from your submit button to your forms onsubmit ;编辑:如果您不希望它提交表单,只需添加一个return false ,但是您需要将您的onclick从您的提交按钮更改为您的 forms onsubmit

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

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