helloise Posted May 30, 2011 Share Posted May 30, 2011 i have the following code: if (($has_freechat1['confirmed'] == 1) && ($has_freechat2['confirmed'] == 1)) { $was_clicked = 0; ?> <a href="#" value="<?php echo $was_clicked; ?>" class="charcoal_link" style="line-height: 20px;" name="message" id="message" onClick="return sendMessage('<?php echo $was_clicked; ?>')"> <?php echo $uniqueCode1?><span class="pink_text"><?php echo $uniqueCode2?></span><?php echo $uniqueCode3?> </a> <?php if ($was_clicked == 1) { ?> <tr> <td><input name="password" type="textarea" rows="10" cols="20" value="<?php echo $aCleanPost['message']['value']?>" /></td> </tr> <tr> <td><br /><input name="cmdSubmit" type="submit" value="Send" /><br/> </td> </tr> <?php } } then i have my JS: <script type="text/JavaScript"> function sendMessage(val) { document.write(val); val = 1; //was clicked return val; } </script> if the link was clicked, i call my JS and set return value = 1 then i check if $was_clicked ==1 if it is i display textarea and a submit button my js is not executing can some one help please? thank you Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/ Share on other sites More sharing options...
seanlim Posted May 30, 2011 Share Posted May 30, 2011 Hi helloise, if the link was clicked, i call my JS and set return value = 1 then i check if $was_clicked ==1 if it is i display textarea and a submit button Your Javascript code won't be able to interact/pass variables to PHP, apart from generating another HTTP request e.g. through Ajax. I can't make any sense of your coding logic, but from what I see, there 2 key problems: 1. $was_clicked is never changed throughout your script. It was declared 0 and will always be zero, rendering your if statement useless. Basically, the if block testing for $was_clicked==1 will never run. 2. Because you set val = 1, and immediately return val, return sendMessage() will ALWAYS return 1, causing the link to be followed through (albeit to "#") Hopefully this will help you figure out where you are going wrong with your logic. Otherwise, please describe what you are trying to achieve, and explain how you know that your "JS is not executing". Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222250 Share on other sites More sharing options...
helloise Posted May 30, 2011 Author Share Posted May 30, 2011 thank you all i want is: if i click on the link i want the textarea and the submit button to appear so that the user can type in a message and click submit is JS then not the route to go? how can i then check if i clicked the link or not? i know my JS is not executing because i had an alert and document.write in there and not either "showed" how must i do this then please? i am still new at all this please help? Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222253 Share on other sites More sharing options...
seanlim Posted May 30, 2011 Share Posted May 30, 2011 Yes, JS is the route to go. However, in your script, you seem to be mixing up JS and PHP. A stripped down version of what you are trying to achieve is this: <a href="#" onclick="document.getElementById('message_area').style.display='block'; return false;">Link</a> <form id="message_area" style="display:none"> <textarea rows="10" cols="20" name="message"></textarea> <input type="submit" name="submit"> </form> I'm not 100% certain if all browsers especially the older ones support the use of the style attribute (haven't used it in awhile), but the general idea is there. Should be able to get you started at least. Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222285 Share on other sites More sharing options...
helloise Posted May 30, 2011 Author Share Posted May 30, 2011 thank you i have it like this now my next issue is: i have a foreach loop that displays multiple links, one for each user the textarea and submit button displays is one place,regardless of the user i clicked on i would have liked it to display underneath the user i clicked on???? would this be possible? thank you Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222297 Share on other sites More sharing options...
seanlim Posted May 30, 2011 Share Posted May 30, 2011 Yes, it's possible. Go read up a little on manipulating DOM with Javascript. <script type="text/javascript"> function showMessageArea(link) { var message_area = document.getElementById('message_area'); message_area.parentNode.removeChild(message_area); link.parentNode.insertBefore(message_area, link.nextSibling); message_area.style.display="block"; } </script> <style type="text/css"> a{display:block} </style> <a href="#" onclick="showMessageArea(this); return false;">Link1</a> <a href="#" onclick="showMessageArea(this);return false;">Link2</a> <a href="#" onclick="showMessageArea(this); return false;">Link3</a> <form id="message_area" style="display:none"> <textarea rows="10" cols="20" name="message"></textarea> <input type="submit" name="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222302 Share on other sites More sharing options...
helloise Posted May 30, 2011 Author Share Posted May 30, 2011 absolutely perfect! thank you very much! i will read up on it thank you Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222332 Share on other sites More sharing options...
helloise Posted May 31, 2011 Author Share Posted May 31, 2011 i have a slight problem now: i see it takes the message_area and appends this to my URL, i actually want to go/redirect to another page informing the user that his/her message was sent AND pass on the actual message and useriD to the next page??? my URL looks like this now: www.adress......./freechatcontacts.php?message=testing&Submit=Send&idTo=36 i want it to look like: www.address..../messageSent.php?message=testing&Submit=Send&idTo=36 please help? thank you Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222742 Share on other sites More sharing options...
seanlim Posted May 31, 2011 Share Posted May 31, 2011 Simply add action="/messageSent.php" to the <form> element Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222744 Share on other sites More sharing options...
helloise Posted May 31, 2011 Author Share Posted May 31, 2011 thank you i have done that but it is not passing the id of the user i clicked on though? it passes the wrong one and also how can i append the message please? $id_to = $user['profile_id_contact']; $has_freechat1 = $obj_clean->getFreeChatRow($id_from,$id_to); $has_freechat2 = $obj_clean->getFreeChatRow($id_to,$id_from); if ($has_freechat1 && $has_freechat2) { if (($has_freechat1['confirmed'] == 1) && ($has_freechat2['confirmed'] == 1)) { ?> <a href="#" class="charcoal_link" style="line-height: 20px;" onclick="showMessageArea(this); return false;"> <?php echo $uniqueCode1?><span class="pink_text"><?php echo $uniqueCode2?></span><?php echo $uniqueCode3?> </a> <form id="message_area" style="display:none" method="post" action="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to ?>"> <tr> <td colspan="2"> <input name="message" type="textarea" rows="10" cols="20" value="<?php echo $aCleanPost['message']['value']?>" /> </td> <td> <input name="Submit" type="submit" value="Send" /> </td> </tr> </form> <?php } } Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222746 Share on other sites More sharing options...
seanlim Posted May 31, 2011 Share Posted May 31, 2011 In messageSent.php, you will be able to access the contents of 1. the message through $_POST['message']. 2. the ID through $_GET['id']. Do those work? Otherwise try var_dump($_POST); var_dump($_GET); in messageSent.php and post the output here Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222754 Share on other sites More sharing options...
helloise Posted May 31, 2011 Author Share Posted May 31, 2011 nope not in messageSent.php i have: $id_to = $_POST['id']; $message = $_POST['message']; then i do this just to see print_r($_SESSION); print_r($_POST); var_dump($_POST); var_dump($_GET); and my output on the screen then gives me: Array ( [image_width] => 200 [user_id] => 64 [user_name] => mimi ) NULL array(1) { ["id"]=> string(2) "36" } i clicked on the user with id 38 not 36 though user_id is correct(this is the id of the user SENDING the message, "id" should be the user the message is going to thus 38 Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222757 Share on other sites More sharing options...
seanlim Posted May 31, 2011 Share Posted May 31, 2011 This is no longer a Javascript problem... var_dump($id_to) on the first page and see if the id is correct before you submit the form. Has $_POST worked on your server before? Is messageSent.php manipulating $_POST in anyway before you var_dump($_POST)? Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222767 Share on other sites More sharing options...
helloise Posted May 31, 2011 Author Share Posted May 31, 2011 1. yes var_dump($id_to) gives correct ids for each link 2. i usually do a js call with onclick and from there do a window.location.href but this particular code is for a mobi site and the same does not work 3. no 4. ok i will post in the php thread thank you for all the help Quote Link to comment https://forums.phpfreaks.com/topic/237840-my-js-not-executing/#findComment-1222772 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.