Jump to content



Photo

Php And Javascript

php javascript jquery twitter

  • Please log in to reply
4 replies to this topic

#1 MrJames

MrJames

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationLondon, United Kingdom

Posted 12 April 2012 - 12:24 PM

Hi,

I have a couple of questions regarding PHP and JavaScript.

1) What I would like to know is once I do a SELECT COUNT Query I want to then do a For-Loop to out put the same number of input boxes but with a PHP variable as the value so that JavaScript could pick that value up,

Is that possible to be done, I mean can JavaScript/JQuery read PHP variables.

They will be Hidden Input boxes as well.


PHP 6
MySQL 5.2
HTML5
CSS3
Chrome / IE 8
  • 0

#2 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 2,699 posts
  • LocationColumbus, OH USA

Posted 12 April 2012 - 6:45 PM

Yes, you can. I can't answer your question right this second, so if someone else doesn't in the meantime, I will answer it in a bit.
  • 0

#3 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 2,699 posts
  • LocationColumbus, OH USA

Posted 12 April 2012 - 11:02 PM

There are two basic ways you can send PHP variables to JS/jQuery. One is to write JS/jQuery using echo statements in PHP tags, and the other is Ajax.

For the first method, just like HTML, you can write JS/jQuery using PHP. For example:

<?php

$apples = 5;

echo '<script>

var apples = ' . $apples . '

alert(apples); // Outputs 5 to a dialog box.

</script>';

?>

That is the "simpler" method, but I find it to be a bit of a workaround and not ideal. Ideally, you should use Ajax to send data back and forth between JS/jQuery and PHP scripts.

I don't want to get into a long discussion of Ajax here, but Larry has an entire (not too long) book on the subject, and his new JS book also talks about Ajax.

If you have any other questions, please let us know. Thanks.
  • 1

#4 MrJames

MrJames

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationLondon, United Kingdom

Posted 13 April 2012 - 6:36 AM

Hi,

What I got is a log in screen, they enter their username and password then they are redirected to a screen that shows their twitter details/friends etc etc.

So I have my SQL SELECT QUERY, I get back a number and I also want to select the users twitter name and put it into a Input box like below:

* This is just an example*

$num = mysqli_num_rows;
$TwitterUsername = row[1];

Then I would echo out a range of input boxes as below:

for(i=$num;i<$num;i++) {

echo '<input type="hidden" value="' . $TwitterUsername . '">';
}

Something along that line, then the JQuery would call Values from the Input boxes if that makes sense, is that possible, or would I be better using a while loop ?

Appreciate any Feedback...

Regards

PHP 6
MySQL 5.2
HTML5
CSS3
Chrome / IE 8
  • 0

#5 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 2,699 posts
  • LocationColumbus, OH USA

Posted 13 April 2012 - 6:56 AM

Yeah, what you want to do makes perfect sense. If you want to use Ajax, you can, but in your case, you don't need it.
The simplest solution is to simply use PHP to write the HTML to the screen, like you're suggesting, and then somewhere below that part in your code, include script tags with the JS/jQuery necessary to make things happen.

As a rough example:

// HTML here

<?php

$num = mysqli_num_rows;
$TwitterUsername = row[1];

for(i=$num;i<$num;i++) {

  echo '<input type="hidden" value="' . $TwitterUsername . '">';

}

?>

// More HTML here

// Include jQuery library here

<script>

  var inputs = $('#form-ID input'),
	hiddenInputs = [];

  for (var i = 0, len = inputs.length; i < len; i++) {

	if (inputs[i].getAttribute('type') === 'hidden') {

	  hiddenInputs.push(inputs[i]);

	}

  }

  // Now use the hiddenInputs array of the input DOM objects you want to do whatever.

</script>

I think that's what you're asking for, but please let me know.
  • 1