PHP question and JQuery

soulblader_86

Supremacy Member
Joined
Sep 28, 2007
Messages
9,012
Reaction score
0
I want to declare something call $num in PHP page


I create a dropdown list with 3 differnet option

How to change the $num through 3 selection dynamica;;y with page refresh?

like if I select option 1

$num = 1;

if I select option 2

$num = 2;



I need to get the value in PHP

I cannot find any tutorial on it

Please help
 

exim22

Member
Joined
Feb 7, 2007
Messages
374
Reaction score
0
Google "jquery select list page refresh", try to include "jquery" keyword and you will find the answers.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
I want to declare something call $num in PHP page


I create a dropdown list with 3 differnet option

How to change the $num through 3 selection dynamica;;y with page refresh?

like if I select option 1

$num = 1;

if I select option 2

$num = 2;



I need to get the value in PHP

I cannot find any tutorial on it

Please help

Across multiple page requests in PHP is normally done via the session and/or database. PHP session can be either file or database backed. Advanced approach can be memcached backed.

There is no need to declare upfront in PHP. PHP uses implicit declaration. There first time you use a variable would bring it into scope.

Official reference to PHP sessions PHP: Sessions - Manual
Tutorials:
PHP Tutorial - Session
PHP Sessions - Learn Sessions PHP - PHP Sessions Tutorial

Sessions are tracked via cookies stored on the browser. Data stored in the session are not transmitted to the browser, only a session token. Actual data are stored in the server via files or database or memcached.

For persistent across sessions, you may opt for HTTP Cookies, but there are some limitations and you have to be mindful about security. Everything passed to the client is always a good source for security breach. That's why sessions are kept internally within the server.

Have issue understanding, post more.
 

soulblader_86

Supremacy Member
Joined
Sep 28, 2007
Messages
9,012
Reaction score
0
PHP:
<?php

	

	if(isset($_REQUEST['data']))
	{
		echo $number = $_REQUEST['data'];
	}
?>
<html>

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script>

var userChoice
var dataString

function output()
{

	userChoice = document.getElementById("blah").value
	dataString = "data=" + userChoice

	document.getElementById("outputText").innerText = userChoice

	$.ajax
	({
		type: 'POST',
		url: 'index.php',
		data: dataString,
		success: function(data)
		{

		}
	})
}

</script>

<body> 
<select id="blah" onChange="output()"><option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select> 

<p id="outputText"></p>




</body>

</html>

I am not able to echo $number;

why uh?

I really google until cannot google

please try out my code and tell me

what shld I do?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
PHP:
<?php

	

	if(isset($_REQUEST['data']))
	{
		echo $number = $_REQUEST['data'];
	}
?>
<html>

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script>

var userChoice
var dataString

function output()
{

	userChoice = document.getElementById("blah").value
	dataString = "data=" + userChoice

	document.getElementById("outputText").innerText = userChoice

	$.ajax
	({
		type: 'POST',
		url: 'index.php',
		data: dataString,
		success: function(data)
		{

		}
	})
}

</script>

<body> 
<select id="blah" onChange="output()"><option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select> 

<p id="outputText"></p>




</body>

</html>

I am not able to echo $number;

why uh?

I really google until cannot google

please try out my code and tell me

what shld I do?

What are you trying to do ? Is this piece of PHP code index.php ? This is not the right way to perform ajax code.

First, you "normally" don't do such re-entrant code. If this piece of code is index.php, you are trying to confuse yourself. For your level, I highly recommend your ajax target script to be another php script that only give output pertaining to what you are interested in.

If this script is index.php, then the ajax code will output everything in HTML as the response back to the ajax.

Another thing is in your ajax function call parameters, you have not fill in what happens to the response data when it is back. What do you want to do with it ? Fill up the "success" callback function with instructions on how you want to perform job on the returned data.

UPDATE:
Just a small update. Avoid using code.jquery.com. This domain name link to edge servers that is in the US. Perhaps MediaTemple don't have edge servers near Singapore. Use the Google CDN instead.
Read more at https://developers.google.com/speed/libraries/devguide
 
Last edited:

soulblader_86

Supremacy Member
Joined
Sep 28, 2007
Messages
9,012
Reaction score
0
What are you trying to do ? Is this piece of PHP code index.php ? This is not the right way to perform ajax code.

First, you "normally" don't do such re-entrant code. If this piece of code is index.php, you are trying to confuse yourself. For your level, I highly recommend your ajax target script to be another php script that only give output pertaining to what you are interested in.

If this script is index.php, then the ajax code will output everything in HTML as the response back to the ajax.

Another thing is in your ajax function call parameters, you have not fill in what happens to the response data when it is back. What do you want to do with it ? Fill up the "success" callback function with instructions on how you want to perform job on the returned data.

UPDATE:
Just a small update. Avoid using code.jquery.com. This domain name link to edge servers that is in the US. Perhaps MediaTemple don't have edge servers near Singapore. Use the Google CDN instead.
Read more at https://developers.google.com/speed/libraries/devguide

I just want to get the value without refresh

sorry but I dun understand jquery at all

It will be better if you could show me with an example

if not watever u say I am just.....:s22: I ma just a novice in jquery...

I only know I cannot refresh and must get the value,

because I have to do a condition display of different things....

long story

Can you just show me a simple example? :s22:
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
I just want to get the value without refresh

sorry but I dun understand jquery at all

It will be better if you could show me with an example

if not watever u say I am just.....:s22: I ma just a novice in jquery...

I only know I cannot refresh and must get the value,

because I have to do a condition display of different things....

long story

Can you just show me a simple example? :s22:

Perhaps this would help

index.html
Code:
<html>
<head>
  <title>Sample</title>
  <script type="text/javascript"
          src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  <script type="text/javascript">
  function output() {
    $.ajax({
      type: 'POST',
      url: 'workonsomething.html',
      success: function(data) {
        alert(data);
      }
    });
  }
  </script>
</head>
<body>
  <input type="button" value="Click Me" onclick="output()"></input>
</body>
</html>

workonsomething.html
Code:
THIS IS MY MESSAGE ?
 

soulblader_86

Supremacy Member
Joined
Sep 28, 2007
Messages
9,012
Reaction score
0
Perhaps this would help

index.html
Code:
<html>
<head>
  <title>Sample</title>
  <script type="text/javascript"
          src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  <script type="text/javascript">
  function output() {
    $.ajax({
      type: 'POST',
      url: 'workonsomething.html',
      success: function(data) {
        alert(data);
      }
    });
  }
  </script>
</head>
<body>
  <input type="button" value="Click Me" onclick="output()"></input>
</body>
</html>

workonsomething.html
Code:
THIS IS MY MESSAGE ?

I still cannot get what I want, thank your help anyway

I cannot get to return a value dynamically...I feel so tired, good nite
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
I still cannot get what I want, thank your help anyway

I cannot get to return a value dynamically...I feel so tired, good nite

Guess you really need handholding huh :)

index.php
PHP:
<html>
<head>
  <title>Sample</title>
  <style type="text/css">
  #myplaceholder {
    width: 200px;
    margin-top: 20px;
    -webkit-box-shadow: 5px 5px 14px rgba(50, 50, 50, 0.75);
    -moz-box-shadow:    5px 5px 14px rgba(50, 50, 50, 0.75);
    box-shadow:         5px 5px 14px rgba(50, 50, 50, 0.75);
  }
  </style>
  <script type="text/javascript"
          src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  <script type="text/javascript">
  function output() {
    $.ajax({
      type: 'POST',
      data: { myvalue: $('#myoptions option:selected').attr('value') },
      url: 'getoutput.php',
      success: function(data) {
        $('#myplaceholder').text(data);
      }
    });
  }
  </script>
</head>
<body>
  <select id="myoptions" onchange="output()">
  <?php
  for ($i = 1; $i <= 3; $i++):
  ?>
  <option value="<?="This is value $i"?>"><?="This is text $i"?></option>
  <?php
  endfor;
  ?>
  </select> 

  <div id="myplaceholder">Waiting for output</div>
</body>
</html>

getoutput.php
PHP:
<?php echo "After ajax call, ".$_REQUEST['myvalue']; ?>

Please ensure yourself that you have spend time reading on jQuery.ajax() | jQuery API Documentation
 

soulblader_86

Supremacy Member
Joined
Sep 28, 2007
Messages
9,012
Reaction score
0
Guess you really need handholding huh :)

index.php
PHP:
<html>
<head>
  <title>Sample</title>
  <style type="text/css">
  #myplaceholder {
    width: 200px;
    margin-top: 20px;
    -webkit-box-shadow: 5px 5px 14px rgba(50, 50, 50, 0.75);
    -moz-box-shadow:    5px 5px 14px rgba(50, 50, 50, 0.75);
    box-shadow:         5px 5px 14px rgba(50, 50, 50, 0.75);
  }
  </style>
  <script type="text/javascript"
          src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  <script type="text/javascript">
  function output() {
    $.ajax({
      type: 'POST',
      data: { myvalue: $('#myoptions option:selected').attr('value') },
      url: 'getoutput.php',
      success: function(data) {
        $('#myplaceholder').text(data);
      }
    });
  }
  </script>
</head>
<body>
  <select id="myoptions" onchange="output()">
  <?php
  for ($i = 1; $i <= 3; $i++):
  ?>
  <option value="<?="This is value $i"?>"><?="This is text $i"?></option>
  <?php
  endfor;
  ?>
  </select> 

  <div id="myplaceholder">Waiting for output</div>
</body>
</html>

getoutput.php
PHP:
<?php echo "After ajax call, ".$_REQUEST['myvalue']; ?>

Please ensure yourself that you have spend time reading on jQuery.ajax() | jQuery API Documentation

Thanks I managed to get the value

but I still some small issue, later then I ask you...:D
 
Last edited:

soulblader_86

Supremacy Member
Joined
Sep 28, 2007
Messages
9,012
Reaction score
0
hi, how to do I load a default value of 1 when I run the page?

now it only work after I select the dropdownlist
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
hi, how to do I load a default value of 1 when I run the page?

now it only work after I select the dropdownlist

Do you mean the default value to show in the div placeholder? Or do you mean the default selected option in the pull down list

For the first case, set it yourself in codes. For the latter case, set "selected" attribute to the option element you want to be as the default during the construction of HTML by php.

You can also do it via JavaScript dynamically on ready event. There are quite a few ways to do that.
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top