-3

I have the following script

 $(document).ready(function(){
   $("#SoumettreCarte").submit(function(ev) {
    alert('ok');
    ev.preventDefault();

    ville="bonjour";

    $.post("../cartes/essai2.php", {ville:ville})
    .done(function (response) {
//         window.location.replace("http://localhost/projet2/cartes/essai2.php");

        });

 });
});

When i click on submit, i want it to redirect me to essai2.php, so i use window.location.replace, but then i lose the value of my $_POST. So my problem, is how to load another page after submitting a form with javascript without losing my $_POST. Thanks for your help.

When i try action="essai2.php", i don't get "ville" in my $_POST.

3 Answers3

1

You can make a hidden field like this:

HTML

<input type="hidden" name="ville" id="ville">

You can change the input field's value like this:

JQUERY

$(document).ready(function(){
  ville = 'Bonjour';
  $("#ville").val(ville);
  //or $("[name=ville]").val(ville);
}
akki
  • 2,021
  • 1
  • 24
  • 35
0

you can do like this

<form action="essai2.php" class ="SoumettreCarte" id="SoumettreCarte" method="post" > 
<input type="text" name="pays" id="pays">
<input type="hidden" name="lat" value=""> 
<input type="hidden" name="lng" value=""> 
<input type="submit" name="submit" id="soumettre" value="soumettre la carte" > 
</form>

in jquery part do like this

var lat = "some lattitude";
var lng = "some longtitude";

$("[name=submit]").click(function(e){

$("[name=lat]").val(lat);
$("[name=lng]").val(lng);
});
krishna
  • 4,069
  • 2
  • 29
  • 56
  • ville is the latitude of the place (i get it with a javascript function), so it's a javascript variable and i don't know how to affect it to my input value. – user3492799 Apr 21 '14 at 09:46
  • With javascript i get 2 variables: latitude and longitude, I ask the user to fill a form and then i want to get the information added by the user, latitude and longitude in my $_POST. – user3492799 Apr 21 '14 at 09:49
  • I found my answer: http://stackoverflow.com/questions/7764154/pass-a-javascript-variable-value-into-input-type-hidden-value – user3492799 Apr 21 '14 at 09:53
0
<form action="essai2.php" class ="SoumettreCarte" id="SoumettreCarte" method="post" >
<input type="text" name="pays" id="pays">
<input type="hidden" name="lat" value="">
<input type="hidden" name="lng" value="">
<input type="submit" name="submit" id="soumettre" value="soumettre la carte" >
</form> 
<script type="text/javascript"> 
 $.post("../cartes/essai2.php", data:$("#SoumettreCarte").serialize()) 
 .done(function (response) {

    });</script>

you can try this one..

Maneesh Singh
  • 555
  • 2
  • 12