A
A
Anton Rodionov2015-09-25 17:34:06
JavaScript
Anton Rodionov, 2015-09-25 17:34:06

Why does a button click reload the page?

Good evening ladies and gentlemen. There is a site on Jumbla and I fasten my module with jQery\AJAX to it. I need that after the user enters data and clicks on the button, php code is executed in the background and eventually tells the user the result without refreshing the page. This is all I did. But what's the problem.

<form>
    <button  onclick="Pusk(document.getElementById('user'),document.getElementById('tel'),document.getElementById('sms'),document.getElementById('to'),document.getElementById('ajax'))">Отправить</button>
</form>

After clicking on the button and performing all the actions, the page reloads.
And the most interesting.
If you hang this onclick, for example, on a regular <Label>, everything works fine. I suspect jumbla is messing up. But, since it’s not my profile, I don’t know which way to start debugging and what is the best way to do it. Advise what?
PS Function code. And I apologize for it. This is my first time working on the web.
function Pusk(_user,_tel,_sms,_to,_ajax)
{  
    
  var user = encodeURIComponent(_user.value);
  var tel = encodeURIComponent(_tel.value); 
  var sms = encodeURIComponent(_sms.value);
  var to = encodeURIComponent(_to.value); 
  ajax = _ajax; 
    var query = 'user='+user+'&tel='+tel+'&sms='+sms+'&to='+to; 
    Request(query);
}

function Request(query)
{
    req.open('post', '/php/1.php' , true);
    req.onreadystatechange = Refresh;
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    req.send(query);  
}

function Refresh()
{
    var a = req.readyState;  
  
    if( a == 4 && req.status==200)
    {   
        var b = req.responseText;
        ajax.defaultValue = b;
        alert(b);
        return(false);
    }
    else
    {  
        ajax.defaultValue = "Отправка";	
    }
}

PS Thanks everyone. As always, the chest just opened. You need to pull up the mattress. :)
<button type="button" onclick="Pusk(document.getElementById('user'),document.getElementById('tel'),document.getElementById('sms'),document.getElementById('to'),document.getElementById('ajax'))" return false;">Отправить</button>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
littleguga, 2015-09-25
@Viper029

function Pusk(...){
    e.preventDefault();
//дальше Ваш код

L
lyeskin, 2015-09-25
@lyeskin

And who will provide the code for your Start function? Well, I'm guessing at random that you don't return false at the end of the function.

M
Maxim E, 2015-09-25
@creativeworm

add return false to the end,
but it is better to specify an id for the button, and put the js code in the header.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question