0

Some part of js function : nothig sent to database, and i've tested t1, t2 & t3 by alert, they're not empty, Any solutions, thanks.

else{
		  var t1 = document.getElementById('t1').value;
		  var t2 = document.getElementById('t2').value;
		  var t3 = document.getElementById('t3').value;
		  window.location.href = "b1.php?t1=" + t1+ "&t2=" + t2 + "&t3=" + t3 ;
		  alert( 'success' ); 
  }
//b1.php

<?php
  
        $email=$_GET["t1"];
    
	$img=$_GET["t2"];
	
        $target=$_GET["t3"];
	
        include("connection.php");
        $sql=mysql_query("insert into b1(email, img, target)
         values ('$email', '$img', '$target')");
   
   
?>

//connection.php

<?php
        mysql_connect('localhost','root','');
        mysql_select_db("banner");
?>

note : i use wampserver for testing

6
  • What does $_GET output for you? Commented Apr 25, 2016 at 21:08
  • try on window.location = "www.yourwebsite.com/b1.php?t1=" + t1+ "&t2=" + t2 + "&t3=" + t3 ; Commented Apr 25, 2016 at 21:09
  • @king bia : As you are using GET method to pass data to your php page so you can properly see if the values are passed in the URL??? So are they passed or not?? Commented Apr 25, 2016 at 21:10
  • 2
    You are vulnerable to sql injection attacks Commented Apr 25, 2016 at 21:11
  • @ShahKhalid : Make sure to include http:// also at the begining of the url or it will be considered a directory under your current domain link..! Commented Apr 25, 2016 at 21:14

1 Answer 1

2

Problem : The values are not submitted through GET Method..!

Note : First I checked the code in Chrome & then in Firefox.Strange thing was that the code was working perfect in Firefox while in chrome it wasn't working at all & was also not working in Internet Explorer etc..!

Solution : I then checked that as you may be submitting the values on a submit so you need to add an event.preventDefault(); to your code..!

Here is the code which I tried and worked right away :

<form action="home.php" onsubmit="redirect(event)">
<input type="text" id="t1">
<input type="text" id="t2">
<input type="text" id="t3">
<input type="submit" >
</form>

<script>

function redirect() {

var t1 = document.getElementById('t1').value;
          var t2 = document.getElementById('t2').value;
          var t3 = document.getElementById('t3').value;
          event.preventDefault();
          alert( 'success' ); 
          window.location.href = "b1.php?t1=" + t1+ "&t2=" + t2 + "&t3=" + t3 ;


}

</script>
Sign up to request clarification or add additional context in comments.

5 Comments

If it's not the case so then update your question with your whole code that exactly how are you submitting these values even in the first place cuz you just shared an else part of the JS Code..!
You need to call the function as onsubmit="redirect(event)". Some browsers don't provide event as a global variable.
@Barmar : Do you mean that's the reason why it was not working in the first place as It's working now without using event with the redirect function??
No, I mean you need to fix that for this answer to be correct.
Your code will work in some browsers, but not others.

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.