简体   繁体   English

从服务器 php javascript 更新动态表单,但需要保持在浏览器的同一页面上

[英]Updating a dynamic form from server php javascript but needs to stay on the same page in browser

I have to create a form where mid way when the user is filling the input on filling one specific select input I need to send a request to the server where I tell the server which select value was selected and then the server will respond with a new page updating the form with additional input fields related to the selected option the user has chosen.我必须创建一个表单,当用户填写一个特定的 select 输入时,我需要向服务器发送一个请求,告诉服务器选择了哪个 select 值,然后服务器将以新的响应页面使用与用户选择的选项相关的附加输入字段更新表单。

I am doing this and all is fine but when the server sends back the updated page the browser is stacking the pages.我正在这样做,一切都很好,但是当服务器发回更新的页面时,浏览器正在堆叠页面。 By stacking I mean that after receiving the updated form if the user presses back he will be taken to the old form that was there before the server call and the update.通过堆叠,我的意思是在收到更新的表单后,如果用户按下,他将被带到服务器调用和更新之前的旧表单。

I don't want this behavior I just want the browser in a way or another to have the same page but with the updated form我不希望这种行为我只是希望浏览器以某种方式具有相同的页面但具有更新的表单

This is the javascript code that is triggered when a select is chosen这是选择 select 时触发的 javascript 代码

    function updateForm(value){
    let form=document.forms["form"];
    let input = document.createElement('input');
    input.setAttribute('name', "typeOfCall");
    input.setAttribute('value', 'updateForm');
    input.setAttribute('type', 'hidden');
    form.appendChild(input);
    form.submit();
}

then the server does a lot of stuff accessing the database then does the following to send back an updated page然后服务器做了很多访问数据库的事情然后执行以下操作以发回更新的页面

    public function render_view($view,$params=[]){
    $layout = $this->get_layout();
    $view = $this->get_view($view,$params);
    return str_replace("{{content}}",$view,$layout);
}

The result of this function is then echoed然后回显这个 function 的结果

I am not using any framework because part of the asignment is not to use any framework我没有使用任何框架,因为部分任务是不使用任何框架

Without using any external libraries you can use JavaScript Fetch API to call the server and retrieve your information:在不使用任何外部库的情况下,您可以使用 JavaScript Fetch API 来调用服务器并检索您的信息:

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

Fetch is asynchronous, so you can update your form using the response once it has been received. Fetch 是异步的,因此您可以在收到响应后使用响应更新您的表单。 You can then parse and use this information to populate/edit the form.然后,您可以解析并使用此信息来填充/编辑表单。

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

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