Dinorah Tovar Dinorah Tovar - 3 months ago 10
Ajax Question

Ajax, just sending value not showing page

I have two files with php and html. This is the basic info inside my A.php file

<div class="col-md-8">
<input type="email" class="form-control" placeholder="Ingresar Número de Nota" id="idnote" name="idnote">
</div>
<button type="button" class="btn btn-primary" id="notes">Enviar</button>


When the user click in the button, it calls a function inside a js.

$(document).ready(function() {
var formData = {
'id' : $('input[name=idnote]').val()
};
$.ajax({
type : 'POST',
url : '../consults/findnote.php',
data : formData,
dataType : 'json',
encode : true
})
.done(function(data) {
//Going to findnote.php and keeping formData
//This findnote.php contains a completely different design but I need the first value to make some changes for the user.
})

});
});


The problem is that after the ajax, my webpage is not going to findnote.php is just sending the value, And I need to show findnote.php
I know if i use

event.preventDefault();


Ajax will prevent the reload, but I'm not using it.

There is a way to do it?
How Do I keep values after making a window.location? (Because if I call the file after the successful call I lose the value)
Should I try with only php? (Its an option)

Answer

Why don't do pure HTML ?

<form action="../consults/findnote.php" method="POST">
    <div class="col-md-8">
       <input type="email" class="form-control" placeholder="Ingresar Número de Nota" id="idnote" name="id">
    </div>
    <button type="submit" class="btn btn-primary" id="notes">Enviar</button>
</form>

Just added the form tag and edited the name of the input for it complies with your function result.

"The problem is that after the ajax, my webpage is not going to findnote.php is just sending the value" This is basically what ajax is made for :)

With ajax, you can, if you want, append the result to the current page (you didn't in your code. Just sent datas).

The other solution with form tag, the one above, load the result as a new page, like a classic link.

Comments