link open jquery function

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 5099 Views - Last Post: 23 December 2012 - 08:06 AM

#1 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

link open jquery function

Posted 19 December 2012 - 03:06 AM

i want to run ajax_1 function when i click the link in index.php. but when i click nothing happens. may be i miss some thing

index.php
echo"
<button type='submit' onclick='return false' 
   onmousedown='javascript:ajax_1('value');'>tess</button>
";




jquery.js
function ajax_1()
{
	alert("test");
}

This post has been edited by hwoarang69: 19 December 2012 - 04:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: link open jquery function

#2 SpAm101  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 54
  • Joined: 28-November 12

Re: link open jquery function

Posted 19 December 2012 - 04:28 AM

Hi hwoarang69

You're using the same type of quotes in the JS declaration and the HTML, when you shouldn't.

It should be like this:

onmousedown="ajax_1('value')"


You also don't need the javascript: bit.

Lastly, you're passing a value to a function that doesn't take one, so change this:

ajax_1()


to this:

ajax_1(myMessage)


or something to that effect ;)
Was This Post Helpful? 1
  • +
  • -

#3 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: link open jquery function

Posted 19 December 2012 - 04:50 AM

thanks i started to see how it works now,

last question les say i have a zoom.php file. when user go to this page the url is "localhost/ecomm/zoom.php?item_id=4"

zoom.php
$item_id_g = $_GET['item_id']; //get the url value
<button type='submit' onclick='return false' onmousedown='ajax_1();'>Like</button>
<div id='div_1'></div> 



jquery.js
function ajax_1()
{
	var url = 'test.php';
	$.post(url, function(data)
	{
		$('#div_1').html(data).show();
	});
}



here in test.php file i want to get the value of url variable.
test.php
<?php
$item_id_g = $_GET['item_id']; //error item_id undifine

$item_query = mysql_query("SELECT * FROM item WHERE item_id = $item_id_g");  
	$row = mysql_fetch_assoc($item_query);
		$thumb_up_db = $row['thumb_up'];

	echo"$thumb_up_db";
?>



so i can access url vairable in zoom.php. should i try send the url variable to test.php?
Was This Post Helpful? 0
  • +
  • -

#4 SpAm101  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 54
  • Joined: 28-November 12

Re: link open jquery function

Posted 19 December 2012 - 05:09 AM

Hi hwoarang69

Quote

should i try send the url variable to test.php?


Exactly that :)

Using your current code, you would do so, like so:

$item_id_g = $_GET['item_id']; //get the url value
<button type='submit' onclick='return false' onmousedown="ajax_1('<?php echo $item_id_g ?>')">Like</button>
<div id='div_1'></div> 



function ajax_1(id)
{
	var url = 'test.php?item_id=' + id;
	$.post(url, function(data)
	{
		$('#div_1').html(data).show();
	});
}

Was This Post Helpful? 1
  • +
  • -

#5 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: link open jquery function

Posted 19 December 2012 - 07:28 AM

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\E_COMMERCE\test.php on line 5

i now wht the error mean that some thing wrong with my sql query. but every thing seem right. and $item_id_g does have a int value.

<?php
$item_id_g = $_POST['item_id_g'];

	$item_query = mysql_query("SELECT * FROM item WHERE item_id = $item_id_g");  
	$row = mysql_fetch_assoc($item_query);
		$thumb_up_db = $row['thumb_up'];

	echo"$thumb_up_db";
?>

Was This Post Helpful? 0
  • +
  • -

#6 SpAm101  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 54
  • Joined: 28-November 12

Re: link open jquery function

Posted 19 December 2012 - 07:34 AM

Hi again #5 hwoarang69

You've changed this line in test.php:

$item_id_g = $_GET['item_id']; //error item_id undifine


to this:

$item_id_g = $_POST['item_id_g'];


You need to change it back :)

The value is being sent through the url, meaning it's a GET request not a POST one.

Also, I forgot to mention before, because of this, you can use $.get instead of $.post :)
Was This Post Helpful? 1
  • +
  • -

#7 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1102
  • View blog
  • Posts: 6,909
  • Joined: 07-September 06

Re: link open jquery function

Posted 19 December 2012 - 10:01 AM

Also, just want to point out that you should sanitize your input before using it in a SQL query. That will greatly reduce the potential problems you have (like someone dropping your tables, or deleting rows). Here are a few threads that will be useful:

Tutorial:
http://www.dreaminco...-to-prevent-it/

Thread:
http://www.dreaminco...ze-post-values/
(NOTE - There are a number of other ones on the same topic)

And the mandatory image:
Posted Image
Was This Post Helpful? 2
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10175
  • View blog
  • Posts: 37,582
  • Joined: 27-December 08

Re: link open jquery function

Posted 19 December 2012 - 10:08 AM

Or just use Prepared Statements like PDO or MySQLi as we've been suggesting in a few of your threads. :)
Was This Post Helpful? 1
  • +
  • -

#9 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1102
  • View blog
  • Posts: 6,909
  • Joined: 07-September 06

Re: link open jquery function

Posted 19 December 2012 - 10:11 AM

Ah, yes. At some point I will have to look into prepared statements, up to this point I have just been using the old mysql_* functions; though it sounds like PHP.net is against that module now that it is pretty old, and will be removing it sometime in the future altogether. Maybe I'll start migrating over the break...
Was This Post Helpful? 1
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10175
  • View blog
  • Posts: 37,582
  • Joined: 27-December 08

Re: link open jquery function

Posted 19 December 2012 - 10:23 AM

Sorry- I meant to direct that comment to the OP. He has a bunch of threads in PHP where Prepared Statements have been suggested a bunch of times. The comments about that are continually ignored.
Was This Post Helpful? 1
  • +
  • -

#11 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: link open jquery function

Posted 20 December 2012 - 01:56 AM

i did tried in mysqli. but was getting a error
Fatal error: Call to a member function fetch_assoc() on a non-object
i was just doing in mysql to see if that makes a difference.

test.php
<?php
$mysql = new mysqli('127.0.0.1', "root" ,'', 'e_commerce');
$result = $mysql->query("SELECT 'thumb_up' FROM 'item' WHERE 'item_id' = 2");
	$row = $result->fetch_assoc();
		print_r($row);
?>

This post has been edited by hwoarang69: 20 December 2012 - 01:58 AM

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10175
  • View blog
  • Posts: 37,582
  • Joined: 27-December 08

Re: link open jquery function

Posted 20 December 2012 - 03:09 PM

Check to make sure $result received a valid object before invoking fetch_assoc(). It probably didn't, which is why you are getting the error.
Was This Post Helpful? 1
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,538
  • Joined: 12-December 12

Re: link open jquery function

Posted 20 December 2012 - 03:33 PM

<?php
$mysql = new mysqli('127.0.0.1', "root" ,'', 'e_commerce');
$result = $mysql->query("SELECT 'thumb_up' FROM 'item' WHERE 'item_id' = 2");
	$row = $result->fetch_assoc();
		print_r($row);
?>


Those seem to be apostrophes ' around the field-names, they should be back-ticks `.
Was This Post Helpful? 1
  • +
  • -

#14 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: link open jquery function

Posted 22 December 2012 - 12:23 PM

yeahh finally got it to working.

thanks alot guys :)
Was This Post Helpful? 0
  • +
  • -

#15 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: link open jquery function

Posted 23 December 2012 - 01:16 AM

is there a way to send php variable to js click function? so soon as user click the button than $num should go to js.

i was thinking making a hidden text field and store the $num in there. than in js i can get the val of hidden text by:
var n = hiddenfield.val();



but is there a better way to do this?

<?php
$num = 3;

echo"<button type='submit' id='like_button' class='button' name='like_button'>Like</button>";
?>




$('#like_button').click(function()
{
	//here i want to get the value of $num
		
});

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2