Click here to Skip to main content
14,493,507 members
Rate this:
Please Sign up or sign in to vote.
See more:
Have a form where if one enters a number, it calls back data from the mysql table based on that number, then writes the data to the page w/out reloading the whole page. Even tho the ajax is working and showing "Success", the PHP isn't getting any value.
All the code in one the same page (ajax and PHP), and the URL for the Ajax is the page itself.

What I have tried:

<pre><!DOCTYPE html>

<html>
	
	<script src="Js/jquery-3.4.1.js"></script>

	<body>
	
		<form method="post" id="myForm" name="myForm" action="" enctype="multipart/form-data" > 	
				
			<input type="Submit" value="submit"  style="visibility:hidden"/>				
			<input type="text" name="square" id="square" size="2" onchange="show()">			
			<input type="hidden" name="submitType" id="submitType">
	
		</form>   
		
		<script type="text/javascript">
		
			function show() 
			{	
				document.getElementById("submitType").value="Show";	

				$(document).ready(function()
				{
					var square=$("#square").val();	
					alert(square)
					var submitType=$("#submitType").val();
			  					
					$.ajax
					({		
						url: "help.php",									
						type: "POST",			
										
						data:
						{
							submitType:"Show",
							square:square		
						},
						
						cache:"false",
						dataType:"html",
						
						error: function (xhr, ajaxOptions,errorThrown)
						{						
							alert("ERROR");
						}, 
				
						success: function (data)
						{            
							alert("Success");
						}					
					});
					
					return false; 
				});						 
			}
		</script>

		<?php
			
			if(isset($_POST["submitType"])=="Show")
			{
				include "mysql.php";  	
							
				$square=$_POST["square"];	

				print_r($square);				
				
				$sqlQuery="Select links from myTable where number='$square'";
				mysqli_query($conn, $sqlQuery) or die(mysqli_error($conn));					
				$result = mysqli_query($conn,$sqlQuery);
				$array = mysqli_fetch_row($result); 					
				print_r("<script type=\"text/javascript\">");	
				print_r("currentSquare=".$square.";");
				print_r("info='".$array[0]."';");			
				print_r("</script>");					
			}

		?>
		
	</body>

</html>
Posted
Updated 16-Dec-19 2:07am
Comments
Member 13358514 15-Dec-19 14:51pm
   
FYI, have tried:

1)document.getElementById("myForm").submit(); in place of the document.ready
- no change

2)$("#myForm").on("submit", function (even); in place of the document.ready
and added event.preventDefault();
- still no change

3) document.getElementById("myForm").submit
- reloaded the whol page

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You say that the message "Success" is displayed which means your success function is running. However, you don't do anything else in the success function. That would be where you change a value on the page.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100