Search code examples
phpjavascripthttp-redirecthref

Dynamic links/redirect with javascript/php


Having an annoying small problem. Cannot find the solution that works tried just about everything i could find from searching here and google.

Purpose of this is to pass them along into a "room" that has been created previously.

Seems like it doesn't matter what i try i cannot get it to load into another page using onclick with an href. And i know its an easy fix its just something silly i cannot think of.

and sorry if i am not posting my code just right this is my first time asking a question i normally just lurk around for answers.

//..Left out <?php and my connect info but it is in my script
//--CLEANUP MY MESS
$sql = "SHOW TABLES";
$result = mysql_query($sql) or die('err11');
while ($row = mysql_fetch_row($result)) $testing[$row[0]] = true;// Gets a list of tables in the database and turns them into a handy format
if ($testing['prim']){// Checks to see if table prim exists
    $sql = "SELECT * FROM prim"; // Pulling all info again after cleaning
    $result = mysql_query($sql) or die('err11');
    $_e = '';
    while ($row = mysql_fetch_assoc($result)){// Cycle through enteries to see what rooms are up
        $_e = $_e . "<a href='' onclick='join(" . $row['room'] . ");'>" . $row['teach'] ."</a><br>";
    }
}else $_e = "Sorry no rooms are open";
mysql_close($con);
?>
<!DOCTYPE html>
<html>
<head>
<script>
function join(er) {
    alert('ffs is this even firing');//this is a debug statement i was using... it was firing
    //THE LINE BELOW DOES NOT SEEM TO WORK
    document.location = "http://***late edit to get rid of the web address lol sorry***start.php?name=" + document.getElementById("name").value + "&room=" + er;
    //THE LINE ABOVE DOES NOT WORK
}
</script>
<title>Portal</title>
</head>
<body>
Name:<input type="text" id='name' name="name"><br><?php echo $_e ?>
</body>
</html>

I tried many different small variations like window.location window.location.href etc etc.. also messed with returns and just driving me nuts Grateful for any help and you folks have a nice day


Solution

  • window.open will open a new window for you. (see http://www.javascript-coder.com/window-popup/javascript-window-open.phtml )

    Alternatively you could set the href and use target="_blank". That way you don't have to use javascript so it's more accessible.