admin管理员组

文章数量:1287867

I want to change php variable ($start and $end called by var b1 and var b2) if the user clicks on button. Now I know that php is server side, but how can I do it? I read something about using $get but I don't know how to implement it:

<?php if ( is_user_logged_in() ) { ?>
<input type="submit" value="Start Chat" id="start_chat" style="position: absolute; top: 30px; left: 10px;" />
<?php
 } ?> 

 <script>
jQuery('#start_chat').click(function(){
  $(this).data('clicked', true);
});

var b1 = '<?php echo $start; ?>';
var b2 = '<?php echo $end; ?>';


if(jQuery('#start_chat').data('clicked')) {
   // change var b1 and b2
} else {
    // do not change anything
}


</script>

<div id="eu_la">
<?php
$start = strtotime('9:30');
$end = strtotime('12:30');
$timenow = date('U'); 
if((date('w') == 4) && ($timenow >= $start && $timenow <= $end)) { // day 2 = Tuesday
  include('facut_mine.php');
   } 
  else {
do_shortcode('[upzslider usingphp=true]');
  } ?>

I want to change php variable ($start and $end called by var b1 and var b2) if the user clicks on button. Now I know that php is server side, but how can I do it? I read something about using $get but I don't know how to implement it:

<?php if ( is_user_logged_in() ) { ?>
<input type="submit" value="Start Chat" id="start_chat" style="position: absolute; top: 30px; left: 10px;" />
<?php
 } ?> 

 <script>
jQuery('#start_chat').click(function(){
  $(this).data('clicked', true);
});

var b1 = '<?php echo $start; ?>';
var b2 = '<?php echo $end; ?>';


if(jQuery('#start_chat').data('clicked')) {
   // change var b1 and b2
} else {
    // do not change anything
}


</script>

<div id="eu_la">
<?php
$start = strtotime('9:30');
$end = strtotime('12:30');
$timenow = date('U'); 
if((date('w') == 4) && ($timenow >= $start && $timenow <= $end)) { // day 2 = Tuesday
  include('facut_mine.php');
   } 
  else {
do_shortcode('[upzslider usingphp=true]');
  } ?>
Share Improve this question edited Sep 20, 2012 at 9:06 Claudiu Creanga asked Sep 20, 2012 at 8:57 Claudiu CreangaClaudiu Creanga 8,38612 gold badges77 silver badges116 bronze badges 3
  • w3schools./php/php_forms.asp – theintersect Commented Sep 20, 2012 at 9:01
  • What variable are you trying to change? I cannot figure out exactly what you are trying to acplish here. – Vlad Commented Sep 20, 2012 at 9:03
  • $start and $end which are called by var b1 and var b2 – Claudiu Creanga Commented Sep 20, 2012 at 9:04
Add a ment  | 

3 Answers 3

Reset to default 6

on your current code here, add:

// change var b1 and b2
$.ajax({
    type: "POST",
    url: "chat.php",
    data: { b1: "123", b2: "456" }
});

on chat.php:

$start = $_POST['b1'];
$end = $_POST['b2'];

Update:

if you need to load back the data here in javascript:

on chat.php make these changes:

// variables
if (!empty($_POST)) {
    $data['b1'] = $_POST['b1'];
    $data['b2'] = $_POST['b2'];
}

// to not lose them
$_SESSION['chat'] = $data;

// to keep it patible with your old code
$start = $data['b1'];
$end = $data['b2'];

// send the JSON formatted output
echo json_encode($data);

on your client-side code:

// change var b1 and b2
$.ajax({
    type: "POST",
    url: "chat.php",
    dataType: 'json',
    data: { b1: "123", b2: "456" }
}).done(function(data) {
    b1 = data.b1;
    b2 = data.b2;
});

I didn't test that, but hope it works!

First of all, the input tag must be enclosed in a form tag. As such,

<?php if ( is_user_logged_in() ) { ?>
<form method="GET">
<input type="submit" value="Start Chat" id="start_chat" style="position: absolute; top: 30px; left: 10px;" />
<input type="hidden" name="start" value="[WHATEVER YOU WANT GOES HERE]" />
<input type="hidden" name="end" value="[WHATEVER YOU WANT GOES HERE]" />
</form>
<?php
 } ?> 

Then, in the PHP code

if (isset($_GET['start']))
    $start = $_GET['start'];
if (isset($_GET['end']))
    $start = $_GET['end'];

That will set the $start and $end variables to whatever values you submitted through the form. However, your JavaScript code won't work as intended

jQuery('#start_chat').click(function(){
     $(this).data('clicked', true);
});


if(jQuery('#start_chat').data('clicked')) {
   // change var b1 and b2
} else {
    // do not change anything
}

The first piece of code adds an event listener to the #start_chat element. The problem, however, is that the latter piece of code is read and executed(by the interpreter) right after, when data('clicked') is not set. So it will never enter the if branch.

As you say correctly, PHP is server side. This is why you would have to call an Ajax-Send to the server, pushing the new values to the php script. Then you'd have to load the answer that is delivered from PHP via Javascript (Ajax onready) and replace the page (parts) with the newly loaded.

Seems awfully plicated for me just to change start and end times. Just do it directly in JS ;)

本文标签: javascriptchange php variable on click eventStack Overflow