Jump to content

my JS not executing


helloise

Recommended Posts

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

 

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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         
                                }
                            }  

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.