Reaper0167 Posted December 31, 2008 Share Posted December 31, 2008 i have a html page with a form and a php with a script. in the script i have a if/else statement. could i put javascript in the php script inside the if/else so it would display in a div on the html page after a submit button was clicked? Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/ Share on other sites More sharing options...
awpti Posted December 31, 2008 Share Posted December 31, 2008 Yup, just have to use PHP to echo it out first. PHP is server-side, JS is clientside. You can use PHP to spit out javascript which should then be executed by the browser. Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726668 Share on other sites More sharing options...
RussellReal Posted December 31, 2008 Share Posted December 31, 2008 yes you can.. Â E.G.. <? Â if (whatever == lolol) { Â Â ?> <script type="text/javascript"> lalalal </script> Â Â <? Â } Â else { Â Â ?> <script type="text/javascript"> OMG LOL! </script> Â Â <? Â } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726669 Share on other sites More sharing options...
RussellReal Posted December 31, 2008 Share Posted December 31, 2008 sorryy made a mistake above, but I just modified it to make it work Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726670 Share on other sites More sharing options...
Reaper0167 Posted December 31, 2008 Author Share Posted December 31, 2008 okay cool. can the javascript text appear on the html page inside a div? Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726673 Share on other sites More sharing options...
Sesquipedalian Posted December 31, 2008 Share Posted December 31, 2008 Yeah, but whats the point of doing that? Â <? if (...) { echo '<div><script>...</script></div>'; } else { echo '<div><script>...</script></div>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726675 Share on other sites More sharing options...
Reaper0167 Posted December 31, 2008 Author Share Posted December 31, 2008 so the text "lalalal".... i'm looking to place that in div on the html page Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726677 Share on other sites More sharing options...
Reaper0167 Posted December 31, 2008 Author Share Posted December 31, 2008 check out this page... mylahstone.com  just enter any name and password. i don't like the separate page it goes to for the error. and i don't want an alert box to pop up... i know how to do all that. i would like the error to appear on the same page. Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726682 Share on other sites More sharing options...
Reaper0167 Posted December 31, 2008 Author Share Posted December 31, 2008 ooppsss wait one second til i fix it.... sorry  Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726685 Share on other sites More sharing options...
Reaper0167 Posted December 31, 2008 Author Share Posted December 31, 2008 ok, check it out...sorry bout that. Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726688 Share on other sites More sharing options...
RussellReal Posted December 31, 2008 Share Posted December 31, 2008 http://mylahstone.com/login.php?username=jhbnklm%3B&password=tyiokpl&submit=++Log+in++ \ use method="POST" for your user login form.. Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726690 Share on other sites More sharing options...
Reaper0167 Posted December 31, 2008 Author Share Posted December 31, 2008 thanks for the heads up on that... i switched it to post... i always put a div with some text on the html page where i would like the error to appear instead of on the new page. Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726692 Share on other sites More sharing options...
RussellReal Posted December 31, 2008 Share Posted December 31, 2008 i put a random username and a random password  but it says invalid username lol Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726711 Share on other sites More sharing options...
Reaper0167 Posted December 31, 2008 Author Share Posted December 31, 2008 it should because there is no one registered yet.. the error that you got on the new browser window is the one that i am wanting to display on the page that the form is on. Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726715 Share on other sites More sharing options...
RussellReal Posted December 31, 2008 Share Posted December 31, 2008 ok you don't need javascript  do this..  <?php  if (your condition) {   $message = "1 message";  }  else {   $message = "another message";  } ?> <html>  <head>  </head>  <body>   <div><?php echo $message; ?></div>  </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726718 Share on other sites More sharing options...
Reaper0167 Posted December 31, 2008 Author Share Posted December 31, 2008 thanks for the help but now when i click on submit it takes me to the php page and that is empty. here is the code. login.php <?php // log in success or failure if ($count == 1) { $message = "You are now logged in."; Â Â } else { $message = "This username is not registered. Pleaser register first."; } ?> Â and my div code index.html <?php // this page is html not php <div> Â Â Â <div align="center"><?php echo $message; ?></div> </div> ?> Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726729 Share on other sites More sharing options...
Reaper0167 Posted December 31, 2008 Author Share Posted December 31, 2008 so does anyone know how to display this in a div on the original form page? i think i'm going crazy looking all over for this. Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726766 Share on other sites More sharing options...
RussellReal Posted December 31, 2008 Share Posted December 31, 2008 and my div code index.html <?php // this page is html not php <div> Â Â Â <div align="center"><?php echo $message; ?></div> </div> ?> Â get rid of those <?php and ?> tags from around your divs.. and then your problem is solved.. Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726796 Share on other sites More sharing options...
Reaper0167 Posted December 31, 2008 Author Share Posted December 31, 2008 i just put the php tags in there so the code was easier to read. ok,,, here is all the code so far. check out the page if you would like. http://mylahstone.com the red text is where i'm looking to put the success/error message,, not in a seperate browser window. // this is the php page with my form <?php <html> <head> <style> .error { color: #ff0000; } </style> <!-- using javascript to make sure fields are not left empty -->   <script type="text/javascript" language="javascript">   function validate(form1)   {   var valid = true;   if (form1.username.value)   {     document.getElementById('username_error').innerHTML = '';   }   else   {     document.getElementById('username_error').innerHTML = 'Please enter a username.';     valid = false;   }   if (form1.password.value)   {     document.getElementById('password_error').innerHTML = '';   }   else   {     document.getElementById('password_error').innerHTML = 'Please enter a password.';     valid = false;   }   return valid;   }   </script>   <style type="text/css">   <!--   body {   background-color: #FFFFFF;   }   -->   </style>   <title></title>   <style type="text/css">   p.c3 {text-align: center}   div.c2 {text-align: right}   div.c1 {text-align: center}   .style4 {color: #FF0000}   </style> </head> <body>   <p> </p>   <p> </p>   <p> </p>   <p> </p>   <table width="100%" height="36" border="0" cellpadding="0" cellspacing="0">     <tr>       <td height="36">         <div class="c1">           <img src="pics/header.png" alt="header" width="433" height="37">         </div>       </td>     </tr>   </table>   <p align="center">Hi, please log in.</p>   <div>    <div align="center" class="style4">I would like the message to display here instead of on new page.</div>   </div> <form action="login.php" method="post" name="form1" id="form1" onSubmit="return validate(this);"> <table width="100%" border="0" cellspacing="3" cellpadding="0">       <tr>         <td width="39%">           <div class="c2">             <label for="username">Username:</label>           </div>         </td>         <td width="61%"><input name="username" type="text" size="35" /> <span id="username_error" class="error"></td>       </tr>       <tr>         <td>           <div class="c2">             <label for="password">Password:</label>           </div>         </td>         <td><input name="password" type="password" size="35" /> <span id="password_error" class="error"></td>       </tr>       <tr>         <td> </td>         <td><input type="submit" name="submit" id="submit" value=" Log in "></td>       </tr>     </table>   </form> <p class="c3">Don't have a username?   <br>   Click here to register.</p> <!-- using javascript to set focus of the cursor on the username textfield --> <script type="text/javascript" language="javascript">   document.form1.username.focus(); </script> </body> </html> ?>  // this is the php with the script <?php $host = "-------------"; // host name $dbusername = "--------------"; // mysql username $dbpassword = "------------"; // mysql password $db_name = "-----------"; // database name $tbl = "-------------"; // table name // connects to server and members databse. mysql_connect("$host", "$dbusername", "$dbpassword") or die("cannot connect"); mysql_select_db("$db_name") or die("cannot select DB"); // username/password changed to variables. $username = $_POST['username']; $password = $_POST['password']; $sql = "SELECT * FROM $tbl WHERE username='$username' and password='$password'"; $result = mysql_query($sql); // create variable for success/failure message $count = mysql_num_rows($result); // log in success or failure if ($count == "1") {   echo "Thank you for logging in."; } else {   echo "This is not a registered username. Please register before logging in."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726798 Share on other sites More sharing options...
RussellReal Posted December 31, 2008 Share Posted December 31, 2008 oh then do the check @ the top of the php page..  if fail  header("location: index.php?error=".urlencode($message));  then in index.php  put  $_GET['error'] inside the div Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726849 Share on other sites More sharing options...
Reaper0167 Posted December 31, 2008 Author Share Posted December 31, 2008 i think we are getting closer.. after clicking the submit button,,, it keeps us on the index page but the error is displayed up top in the url box. Â Â http://mylahstone.com/index.php?error=This+username+is+not+registered.+Pleaser+register+first. Â I'll keep playing around with it. Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-726856 Share on other sites More sharing options...
Reaper0167 Posted January 1, 2009 Author Share Posted January 1, 2009 bump ttt,,,,, anyone? Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-727293 Share on other sites More sharing options...
RussellReal Posted January 1, 2009 Share Posted January 1, 2009 for the div on the page do: Â <div><? echo $_GET['error']; ?></div> Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-727352 Share on other sites More sharing options...
Reaper0167 Posted January 1, 2009 Author Share Posted January 1, 2009 nothing yet,,,,,i think i already tried putting the div inside a form and setting the form also... but that did not work.... i am out of ideas. i keep thinking it is something in this line of code <?php header("location: index.php?error=" . urlencode($message)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-727357 Share on other sites More sharing options...
Reaper0167 Posted January 1, 2009 Author Share Posted January 1, 2009 i know he is probably getting tired of me but where is RussellReal when you need him, lol. Quote Link to comment https://forums.phpfreaks.com/topic/138942-solved-another-question-for-everyone-who-knows-what-there-doing/#findComment-727473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.