单击“提交”按钮后如何保持选定的下拉值

I want to show selected data after click on submit.I have search too much but not get proper answer.

enter image description here

i used this code but what will be the next step i am not getting this.

if($actiondone!=''){
    echo "<script>
        $('#actiondoc').val();
        </script>";
}

Please guide me for solutuion

Thanks

doujia7162
doujia7162 我已经解决了这个问题
10 个月之前 回复
dongli564510
dongli564510 这是你正在回应的JQuery语句,所以你确定你的解决方案中包含了JQuery吗?您没有标记您的问题。
11 个月之前 回复
dougan4884
dougan4884 是PHP代码吗?请正确指定并标记问题。
11 个月之前 回复

1个回答

You cannot use PHP to interact with the user.

After the page has rendered, PHP is finished. If you want to interact with the user (i.e. detect changes to drop-downs, etc) you must use jQuery/javascript only.

For your example, you need to have the jQuery code pre-existing in the page - that is, the javascript gets written into the DOM as the page is being initially rendered. You can add the jQuery/javascript code as part of the HTML, or you can echo that code in PHP as the page is being built -- but it must be on the page when the user gets control of the DOM.

Working demo:

$(function(){
    $('#form_submit').click(function(){
        alert( $('#actiondoc').val() );
        return false; //this line cancels the SUBMIT action of the form
    });
    
    $('#actiondoc').change(function(){
      alert('You chose: ' + $(this).find('option:selected').text() );
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<form id="your_form">
    <select id="actiondoc">
        <option value="">Choose:</option>
        <option value="chg">change of tyre</option>
        <option value="rnw">renew tyre</option>
    </select>
    <input id="form_submit" type="submit" value="submit" />
</form>


You say that you want to show the selected choice after user clicks on submit, but there might be a problem with that idea:

When user clicks submit, an HTML form will immediately (a) get the values of the form controls and (b) deliver them to the page specified on the <form action="new_page.php" method="post"> -- so, in this code right here, the browser will navigate over to new_page.php and deliver the form values to that page.

The point is: the page will change (unless you are using AJAX - are you?) - so you cannot show anything more on that page because the user isn't on that page any longer.

So, you can show the value of the #actiondoc control on the new page, OR you can intercept the submit function, as I did in my demo above when I displayed the alert()

If you tell us more about exactly what you want to do, we can help more.


References:

https://www.w3schools.com/xml/ajax_intro.asp

AJAX vs Form Submission

YouTube - Simple AJAX Contact Form - HTML, PHP, AJAX

</div>
douhui5953
douhui5953 注意:如果表单标记未在表单标记的action =“”属性中指定其他页面,则页面将只是重新加载。 例如:如果您的表单标记看起来像这样:<form>,那么当用户单击该表单中的提交按钮时,该页面将只重新加载。 如果发生这种情况,现在你知道为什么了。
11 个月之前 回复
dougourang1856
dougourang1856 在这种情况下,页面将从#actiondoc下拉列表的网页切换到<form>标记中指定的页面。 请:(1)发布包含表单标记的HTML,以及(2)告诉我们当用户单击提交按钮时您想要发生什么。 请逐步告诉我们您想要发生的事情(一切)。 谢谢!
11 个月之前 回复
douzhang7728
douzhang7728 我没有使用ajax
11 个月之前 回复
duanduoding2238
duanduoding2238 当我删除返回false它不能正常工作,我需要提交按钮应该工作
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问