1条回答 默认 最新
- dqbr37828 2019-02-18 17:42关注
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 tonew_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 thealert()
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
YouTube - Simple AJAX Contact Form - HTML, PHP, AJAX
</div>
解决 无用评论 打赏 举报
悬赏问题
- ¥15 vs2019中数据导出问题
- ¥20 云服务Linux系统TCP-MSS值修改?
- ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
- ¥20 怎么在stm32门禁成品上增加查询记录功能
- ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
- ¥50 NT4.0系统 STOP:0X0000007B
- ¥15 想问一下stata17中这段代码哪里有问题呀
- ¥15 flink cdc无法实时同步mysql数据
- ¥100 有人会搭建GPT-J-6B框架吗?有偿
- ¥15 求差集那个函数有问题,有无佬可以解决