Link to home
Start Free TrialLog in
Avatar of Yaroslav Vendysh
Yaroslav Vendysh

asked on

php ajax contact form gives me undefined index error upon submission

When i try get ehco from php to ajax, all time i get "Baddd".
Why isset($data['do_login']) all time false?!
Name button is normal..
If i delete ajax function and add in form action="demo.php", then after submit form open new window(demo.php) and write "good".
But with ajax it doesnt work :(
It say error: Undefined index: do_login in demo.php on line 6, but why?!
Help pls

	<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
	  <div class="modal-dialog" role="document">
	    <div class="modal-content">
	      <div class="modal-header">
	        <h4 class="modal-title" id="myModalLabel">Увійти</h4>
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
	        	<span aria-hidden="true">&times;
	        	</span>
	        </button>
	      </div>
	      <div class="modal-body">
	        <form method="POST" class="js-form" id="popupResult">
				<input type="email" name="email" class="form-control" required="" 
				 placeholder="Електронна адреса">
				<input type="password" name="password" class="form-control" required="" 
				 placeholder="Пароль">
				<label>
					<input type="checkbox" name="option1">
					 Запам'ятати мене
				</label>
				<input type="submit" value="Submit" id="do_login" name="do_login"/>
	        </form>
	      </div>
	      <div class="modal-footer">
	      	<!-- <a href="" class="mr-auto ml-auto">
	      		Не отримали листа-підтвердження?
	      	</a> -->
	      <div id="result"></div>
	      </div>
	    </div>
	  </div>
	</div> 

Open in new window


$("#do_login").click( function() {
	        $.ajax({
		        type: "POST",
		        //dataType: "html",
		        data: $("#popupResult").serialize(),
		        url: 'demo.php',
		        success: function(data) {
		        	alert(data);
		        	//$("#result").text(data);
		        	//alert(data);
		            //if (data == true) $("#myModal").modal('hide');
		            //if (data == false) $("#result").text("Bad");
		            //$("#myModal").modal('hide');
		        }
		    });

	    	$("#popupResult").submit( function() {
	       		return false;    
	    	});
	    });

Open in new window


<?php
	require "db.php";
	
	$data = $_POST;

	echo $_POST["do_login"];
	echo $_POST["email"];
	echo $_POST["password"];

	if(isset($data['do_login']))
	{
		$errors = array();
		$user = R::findOne('login', 'email = ?', array($data['email']));
		if($user) 
		{
			if (password_verify($data['password'], $user->password))
			{
				$_SESSION['logged_user'] = $user;
				//echo '<div style="color:green;">Easy</div>';
				echo "good";
			}
			else
			{
				$errors[] = 'Неправильно введения пароль';
			}
		}
		else
		{
			$errors[] = 'Користувача з таким логіном не існує';
		}

		if(!empty($errors))
		{
			//echo '<div style="color:red;">'.array_shift($errors).'</div>';
			echo "er";
		}
	} else echo "Baddd";
?>

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

with your form action="demo.php" trick,
replace your demo.php by :

<?php
    var_dump();
?>

Open in new window


maybe your form is malformed
Avatar of Yaroslav Vendysh
Yaroslav Vendysh

ASKER

@leakim971, when i try dump _POST in demo.php, there only 2 variables: email and password, no button User generated image
on Chrome, do a right click on this do_login button and choose inspect to check  what is going on
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you:)