Summer Sale! All accounts are 50% off this week.

mkumar2001's avatar

Getting form submitted continuously. I want form submitted only once on page load

<form method="post" id="visitor" action="{{ url('addetails',$listing->id) }}" enctype="multipart/form-data"> <input type="hidden" name="lid" value="{{$listing->id}}" /> <input type="hidden" name="product_id" value="{{$listing->product_id}}" /> <input type="hidden" name="ip" value="{{\Request::ip()}}" /> <input type="hidden" name="visit_count" value="1" /> </form>

    document.getElementById('visitor').submit();

I want once submitted it must be stopped

0 likes
17 replies
markus.heb's avatar

This code is not enough to help you with your problem. Please give us some more infos.

mkumar2001's avatar

This is Form

<form method="post" id="visitor" action="{{ url('addetails',$listing->id) }}" enctype="multipart/form-data"> @csrf <input type="hidden" name="lid" value="{{$listing->id}}" /> <input type="hidden" name="product_id" value="{{$listing->product_id}}" /> <input type="hidden" name="ip" value="{{\Request::ip()}}" /> <input type="hidden" name="visit_count" value="1" /> </form>

and this is script

 ```document.getElementById('visitor').submit();```
mkumar2001's avatar

if I am using this then nothing has been stored in database

document.onload=function(){

document.getElementById('visitor').submit();
 event.preventDefault();
document.history.back();

}

mkumar2001's avatar

Thanks for sharing the link for basic learning. But my concern is something else. form need to submitted only once on page load

Sergiu17's avatar

OK, as I understood.

// page loads
// form is submitted
// redirect back

// page loads
// form is submitted again
// redirect back

// infinite loop

Sending an AJAX Request would solve your problem

Snapey's avatar

submitting form will lead to redirect which leads to loading page, which leads to submitting form,etc, etc

You should use ajax... but I dont know what you will post as user has not had chance to supply any data.

Bit of a smell there

Snapey's avatar

and when you are posting code here, use three backticks ``` on a line before and after each code block

oh, and if it does not look right go back and edit your post rather than posting again

1 like
mkumar2001's avatar

Exactly you understood my problem. Could you help me to come out of this. Thanks

munazzil's avatar

You can use onsubmit in below instead of onloadelse can define the time in the function,

    document.onsubmit=function()
    
    {
     document.getElementById('visitor').submit();
     event.preventDefault();
     document.history.back();
     }
mkumar2001's avatar

For this I need click on submit button but I do not want to click on submit button. It should be automatically.

Snapey's avatar

but why? What needs to be submitted that you don't already know

mkumar2001's avatar

Trying to save client IP address so that can be counted the views of particular post. If you see my code you will come to know.

Snapey's avatar

You can just get the client IP from the GET request for the page! Why on earth would you need to send it from the server (in the request object) to the client, in a form, and then post it back to the server. It makes no sense.

Just store client IP in your show method where you return the view.

2 likes

Please or to participate in this conversation.