dp198879 2019-05-30 06:09
浏览 88

如何在每次提交注册表单时从数据库将变量传递给url

I'm working on an online exam site. So when a user submits the registration form on my site, the user must be directed to 'url/accesscode'. Here, the accesscode variable must be sent to the URL from the database when the form is submitted. A new access code is to be sent for every new registration.

How can I accomplish this using javascript/PHP?

I tried to concatenate url using javascript, also tried hidden inputs on forms, but these will work for only single access code.

Method1 - Javascript

var aCode=“accesscode”
function onClick()
{
window.open("url"+aCode);
};

Method2 - HTML

<form action="URL" method="GET">
<input type="hidden" name="access_code" value="accesscode">
<?form>
  • 写回答

1条回答 默认 最新

  • doumu6941 2019-05-30 06:35
    关注

    You can use a couple of functions, like onclickor onsubmit(form function), but there's too a way to do that trough the input using onchangeor onblur, but you have to be beware about the behavior of each one.

    Like:

    onclick it'll be triggered when you click in the element. onblur it'll be triggered every time the focus is out of the element (usually it used in input of text type). onchange it'll be triggered every time the input is changing (when you filling it).

    So following your sample of code the possibilities could be:

    const element = document.getElementById('element');
    const url = "something";
    // using on change
    element.onchange = function() {
        const form = document.getElementById("form");
        form.target.url = url;
    }
    
    // using onblur
    element.onchange = function() {
        const form = document.getElementById("form");
        form.target.url = url;
    }
    
    // using on click
    element.onclick = function() {
        const form = document.getElementById("form");
        form.target.url = url;
    }
    

    Now if you want to use in 'onsubmit' function you need referer the form element, like:

    const form = document.getElementById("form");
    const formUrl = "something";
    form.onsubmit = function(event) {
       // if you want to prevent the default form behavior event.preventDefault();
       event.target.url = formUrl;
    }
    

    After that, you can follow your logic normal.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)