0

Hi I have a problem with post

I wrote some codes with php and jquery here is my code

  

<?php $g= $_POST['g'];  ?>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script>


  
<script>

var g = "<?php echo $g ;  ?>" ;

	$(document).ready(function(){
		if( g == "on" ){
			$.post("URL", "ID=4" )
		}
		if( g == "off" ){
			$.get("URL","ID=4");
		}
});


</script>
</head>
</html>

if I use another php code like below my jquery codes work(g = on status ) because after clicking it loads above codes and load above page

<html>
	<head>
		<meta charset="UTF-8">
		
	</head>
	<body>
		<form action="above.php" method="post">
			<button type="submit" name="g" value="on">OK
		</form>
	</body>

</html>

but if I post it with my android app which just posts g and doesn't load the page it doesn't work :(

3
  • why did you included the jquery library twice? Commented Feb 20, 2017 at 6:37
  • For starters remove the first script element (no need to load jQuery twice). Can you try to rephrase your question, I've a hard time understanding what you're trying to achieve. For example, I'm not sure how the two pieces of code relate to each other. Or which android app you're talking about. Commented Feb 20, 2017 at 6:40
  • sorry I made mistake in entering my codes , look I want to controle my post by using android app and when it posts string to my server codes my server codes recognize my string if it was on post and else get to my defined url Commented Feb 20, 2017 at 7:00

2 Answers 2

2

use callback function to execute the wanted redirection after the post request has been successfull done

ex:

$.post("URL", "ID=4" , function(data, status){
    if (status == "success") { 
        window.location = "http://your-current-url";
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Use Ajax for this purpose. AJAX is nothing but asynchronous JavaScript and XML. It is written just like JavaScript in tags. You can even return partial views using ajax which will not cause page load. Please go and do some R&D on ajax and use it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.