6 Replies - 1427 Views - Last Post: 14 October 2012 - 08:19 AM Rate Topic: -----

#1 martin257  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-October 12

Calling javascript function from php

Posted 12 October 2012 - 04:42 AM

so heres my problem, i am attempting to call a javascript function from php and it kind of works, but its like the function doesn't stay active. When you click a button, the form will become visible after it is made invisible onload. At the moment, when you click the button, the form will pop up but only temporarily, it disappears again after.

heres my code.
Javascript in the header
<script>
		function hideonload() {
			document.getElementById('dialogForm').style.visibility = 'hidden'; //On page load, hide comment box.
			document.getElementById('overlay').style.visibility = 'hidden';
		}

		function showDialog() {
			var dialog = document.getElementById('dialogForm');
			dialog.style.visibility = 'visible'; //Make comment box visible.
			document.getElementById('overlay').style.visibility = 'visible';
			window.location.hash = '#dialogForm' //focus commentDialog box.
		}

		function hideDialog(id) {
			var dialog = document.getElementById(id);
			dialog.style.visibility = 'hidden'; //Make comment box invisible.
			document.getElementById('overlay').style.visibility = 'hidden';
		}
		function init() {
		hideonload();
		}
		window.onload = init;
	</script>


form i want made visible
<form class="dialog" id="dialogForm" action="insert.php" method="post">
    <ul>
        <li>
            <h2>Post your views</h2>
            <span class="required_notification">* Denotes Required Field</span>
        </li>
        <li>
            <label for="main">Main:</label>
            <textarea name="contents" cols="40" rows="6" required ></textarea>
        </li>
        <li>
        	<button class="submit" name="postInThread" type="submit">Post</button>
        	<button class="submit" name="cancel" type="submit" onclick="hideDialog('dialogForm');">Cancel</button>
        </li>
    </ul>
</form>


php code (echo form)
while ($query_row = mysql_fetch_assoc($recent))
	{
	if (isset($_SESSION['loggedin']) && ($_SESSION['loggedin'] == true) && ($query_row['Username'] != $_SESSION['Username'])) {
		echo '<li class="postHeader"><label for="datePosted">Date posted: </label><p>'.$query_row['datePostCreated'].'</p></li><li><label for="main">Main: </label><p>'.$query_row['Main'].'</p></br><label for="username">Posted by: </label><p>'.$query_row['Username'].'</p><button value="'.$query_row['postID'].'" name="reply" onclick="showDialog();">reply</button></li>';
	}
	else
	{
			echo '<li class="postHeader"><label for="datePosted">Date posted: </label><p>'.$query_row['datePostCreated'].'</p></li><li><label for="main">Main: </label><p>'.$query_row['Main'].'</p></br><label for="username">Posted by: </label><p>'.$query_row['Username'].'</p></li>';
			
	}
	}


I don't know what to do next.
Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Calling javascript function from php

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,020
  • Joined: 08-June 10

Re: Calling javascript function from php

Posted 12 October 2012 - 05:00 AM

first thing to remember is that Javascript and PHP do not directly work together. the one is on the client, the other on the server. the only ever connection they have is HTTP.

having said that, do you have a live page of that. there is no indication in your code what else event handlers you have on the page/buttons/form that could cause this behaviour.
Was This Post Helpful? 1
  • +
  • -

#3 MrXtremeM  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 13-October 12

Re: Calling javascript function from php

Posted 13 October 2012 - 03:23 PM

Well,
U can't call javascript from php. But u can do call a function echoing javascript using php. :)
Was This Post Helpful? 0
  • +
  • -

#4 martin257  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-October 12

Re: Calling javascript function from php

Posted 14 October 2012 - 02:45 AM

I kind of found a solution where php is calling the javascript functions but php and javascript are still doing there own job on there own. The php echo form is calling the functions, which is the only link. The original problem, was that it was called but the div didn't stay visible, it was like as soon as it was clicked, the page would refresh.

So heres what i did; the only thing the javascript does is make the div that is created by php hidden or visible when called. I now have statements saying that if the reply button is clicked, then set showForm variable to true. This cannot be changed until another button is pressed. If that variable is true, then created the form using php echo. Else if the variable is false, then don't create the form. The variable would be set to false, if you clicked the cancel button on the form. It will also call another javascript function which will make the invisible again. So php is having the most influence and boolean is the answer; it seemed that it didn't know what to do after it was clicked and by setting it to true indefinetely until told otherwise, that form could appear and stay constant. I will post the code later.

Another problem i have is concerning the while loop which retrieves all the posts in that thread. The code is in the op and when clicked reply, i want that post id to be retrieved and displayed in the form. What would happen is it would do it once and then if you clicked another post, that id wouldn't be retrieved.

Heres my code
if(isset($_POST['reply']))
{
$postid = $_POST['reply'];

Echo $postid;
}


If you click the button, which is identified by its name, then the button value will be retrieved is what i thought.

Any help would be appreciated, thanks. This is the last stumbling block.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,020
  • Joined: 08-June 10

Re: Calling javascript function from php

Posted 14 October 2012 - 03:26 AM

if the button click does not cause the form to be submitted, why should PHP take any action?
Was This Post Helpful? 0
  • +
  • -

#6 martin257  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-October 12

Re: Calling javascript function from php

Posted 14 October 2012 - 05:00 AM

View PostDormilich, on 14 October 2012 - 03:26 AM, said:

if the button click does not cause the form to be submitted, why should PHP take any action?


So what are you saying, that the button should have the type submit or something else?
Was This Post Helpful? 0
  • +
  • -

#7 martin257  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-October 12

Re: Calling javascript function from php

Posted 14 October 2012 - 08:19 AM

Ive done it, javascript takes care of it all, the earlier explanations are not the solution, php requires refresh whilst javascript doesn't; that was my problem.

i can post the code if anyone wants me to, anyway thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1