Jump to content

Onclick won't work at all?


Tenaciousmug

Recommended Posts

<?php include("/home1/elvonica/public_html/intellectproductions/header.php");

?>
			<script type="text/javascript">	
					function validateMe()
					{
						alert("hi!!");
						var password = $('input#password').val();
						alert(password);

						$.ajax(
						{
							type: "GET",
							url: "validation.php?password="+ password,
							data: "password="+ password,
							function(data)
							{
								if(data = "yes")
								{
									$("#post").fadeIn();
									$("#validate").hide();
								}
								else
								{
									alert("Could not validate.");
								}
							}
						});
					}
			</script>

			<h1>Blog Entry</h1>
			<div class="forms">
				<form action="validation.php" method="get" class="blog_form" id="validate">
					<label>Staff Password:
					<span class="small">Only staff can post blogs.</span></label>
					<input type="password" name="password" class="input_text" id="password" />

					<button type="button" class="submit" onclick="validateMe()">Validate Me</button>
				</form>

				<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" class="blog_form" id="post">
					<label>Blog Title:</label>
					<input type="password" name="password" class="input_text" />

					<label class="label_textarea">Body:</label>
					<textarea name="body" class="input_textarea"></textarea>

					<input type="submit" name="post_blog" value="Post Blog" class="submit" />
					<input type="submit" name="prev_blog" value="Preview Blog" class="submit" />
				</form>
			</div>
<?php include("/home1/elvonica/public_html/intellectproductions/footer.php"); ?>

 

As you can see I have a function named "validateMe()" in my javascript code. And then on the onclick of the password validation form, it uses that function. Well I have test alerts saying Hiii and all this weird stuff, but it won't even pop up so it's not even reading my function on the onclick at all...

And I already did the $(document)ready{function()}; or whatever it is and it still didn't work. I have a script similar to using a function on the onclick and I have it all set up. That one works and this one doesn't... With other javascript stuff such as simple hide and show work fine, but whenever I do an onclick, it doesn't work..

Link to comment
Share on other sites

Considering your already using jQuery you should avoid mixing events into your markup all together. JQuery provides a selector engine which is much more elegant a solution.

 

Anyway, have you tried debugging your code in firebug? If so, how? and what results duid you get?

Link to comment
Share on other sites

Wowww never new about Firebug. Figured out my problem immediately! Thanks for that.

But I also have another question. You know how I'm using the onclick? Well I literally have to CLICK the button for it to work. When I press enter, it actually leads to validation.php?password=whatever... How do I make it so that even if I hit enter or click the button, it will make it work properly and not lead to the .php url?

Link to comment
Share on other sites

Instead of onclick on the submit button, attach to the onsubmit event of the form.

Also on your buttons, their 'type' should be set to 'submit' not 'button'.

 

If you wish to use jQuery to attach your events, you could do something along the lines of:

jQuery(function ($) {
    $('#validate').submit(function (e) {
        // validation code...
        // use 'e.preventDefault()' to stop form submission
    });
});

 

Also in your JavaScript you have the following line:

if(data = "yes")

 

Shouldn't this be:

if (data === "yes")

 

Hope this helps.

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.