简体   繁体   English

模态输入字段 (Javascript / xhttprequestheader) 写入 mysql 但仅针对字符串中的 > 0 个字符时发生了奇怪的事情?

[英]Strange thing happening with modal input fields (Javascript / xhttprequestheader) writing to mysql but only for > 0 char in string?

I am using a Flashcards application from GitHub.我正在使用来自 GitHub 的抽认卡应用程序。
I am building this to learn Latin but decided to use existing code.我正在构建它来学习拉丁语,但决定使用现有代码。 It works in Laravel and all good.它在 Laravel 中工作并且一切正常。 I added a Notes field to the application so it shows Front, Back and Notes.我在应用程序中添加了一个备注字段,以便它显示正面、背面和备注。

BUT I am finding that when I edit and update the input fields in the model all good EXCEPT if there is no input left in the fields.但我发现,当我编辑和更新 model 中的输入字段时,一切都很好,除非字段中没有输入。 For any field with more than no characters I get the success message and look at mysql table and the information is there.对于任何多于无字符的字段,我都会收到成功消息并查看 mysql 表,信息就在那里。 But with no character in any of the 3 input fields I do not get the success message and no change in mysql table.但是在 3 个输入字段中的任何一个都没有字符,我没有收到成功消息,mysql 表也没有变化。 This is what I receive back from a dd($card);这是我从 dd($card) 收到的; in the controller update request:在 controller 更新请求中:

Card {#240
  #fillable: array:3 [
    0 => "front"
    1 => "back"
    2 => "notes"
  ]
  #connection: "mysql"
  #table: "cards"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:7 [
    "id" => 3493
    "created_at" => "2023-01-05 11:14:03"
    "updated_at" => "2023-01-07 05:01:20"
    "deck_id" => 1
    "front" => "Zdasdfsadf"
    "back" => "Zasdfsadf"
    "notes" => "Z"
  ]
  #original: array:7 [
    "id" => 3493
    "created_at" => "2023-01-05 11:14:03"
    "updated_at" => "2023-01-07 05:01:20"
    "deck_id" => 1
    "front" => "Zdasdfsadf"
    "back" => "Zasdfsadf"
    "notes" => "Zsdsfg"
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [
    0 => "*"
  ]
}

if a field is empty then I do not get anything back from the dd($card);如果一个字段是空的,那么我不会从 dd($card) 得到任何东西; in controller. <COULD THIS BE RELATED TO THE ISSUE HERE: https://github.com/Laravel-Backpack/CRUD/issues/1816在 controller 中。<这可能与这里的问题有关: https://github.com/Laravel-Backpack/CRUD/issues/1816

The area I am looking at (I beleive is causing the problem within Laravel structure) is Javascript and code is below.我正在查看的区域(我相信在 Laravel 结构中引起了问题)是 Javascript,代码如下。 Ie Controller is set up per link in Github below:即 Controller 是在下面的 Github 中为每个链接设置的:

    function editCard(form){
        let xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            //on success
            if (this.readyState == 4 && this.status == 200) {
                flashMessage(this.responseText); 
                form.reset(); 
                toggleModalCard('modal-edit');
                updateCards();
                //now in updateCards()
                
              /*  document.getElementById('view-front').innerHTML = '';
                document.getElementById('view-back').innerHTML = '';
                delete document.getElementById('view-card').dataset.value;*/
            }
        };
        let data = "?"+"front="+form["front"].value     
        +"&"+"back="+form["back"].value
        +"&"+"notes="+form["notes"].value
        + "&" + "id=" + deck.id;
        let card_id = document.getElementById('view-card').dataset.value;
        xhttp.open('PUT','{!! route('cards.index')!!}'+`/${card_id}`+data, true);
        console.log(data)
        let token =  document.querySelector('meta[name=csrf-token]').content
        xhttp.setRequestHeader('X-CSRF-Token', token);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhttp.send();

The form is below:
    <div id="modal-edit" class="modal">
        {{ Form::open(array('onsubmit' => 'editCard(this); return false;', 'class' =>'modal-content')) }}
            <div class="form-group">
                {{ Form::label('front', 'Front') }}
                {{ Form::text('front', '', ['class' => 'form-control', 'placeholder' => 'Front']) }}
            </div>
            <div class="form-group">
                {{ Form::label('back', 'Back') }}
                {{ Form::text('back', '', ['class' => 'form-control', 'placeholder' => 'Back']) }}
            </div>
            <div class="form-group">
            {{ Form::label('notes', 'Notes') }}
             {{ Form::textarea('notes', '', ['class' => 'form-control', 'placeholder' => 'Notes']) }}
             </div>
            {{  Form::submit('Save changes', ['class' => 'btn btn-primary']) }}
        {{  Form::close() }}
    </div>
    
        <div id="view-card" class="card">
        <div class="card-body">
            <p id="view-front" class="card-text">
            </p>
            <hr>
            <p id="view-back" class="card-text">
            </p>
                <p id="view-notes" class="card-text">
            </p>
        </div>  
        <div id="editCard">
            <button class="btn btn-primary" onclick="toggleModalCard('modal-edit')" data-toggle="tooltip" title="Edit card" onsubmit="if(this == ''){$this.val('empty');}"><i class="fa fa-edit"></i></button>
        </div>
    </div>
</div>

I am stumped: The original code which also does not work correctly is here: https://github.com/Jondev01/flashcards I tried to reach out to the programmer but no feedback.我很难过:也不能正常工作的原始代码在这里: https://github.com/Jondev01/flashcards我试图联系程序员但没有反馈。 Hope someone has seen this before and can make sense of this.希望有人以前见过这个并且能理解这一点。

I try to get input fields with less than 2 characters to save to the mysql database.我尝试获取少于 2 个字符的输入字段以保存到 mysql 数据库。 < 2 does not save but 2 and > does. < 2 不保存,但 2 和 > 保存。

Hi I solved my own problem.嗨,我解决了自己的问题。 I only needed to remove the following:我只需要删除以下内容:

public function update(Request $request, $id)
{
/*    $this->validate($request, [
        'front' => 'required',
        'back' => 'required',
        'notes' => 'required',  
      ]);  
*/

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

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