Jump to content

Weird $_POST problem


4E20

Recommended Posts

Hello people,

 

I am writing this post because since last night im getting a really weird problem with a $_POST form.

I will not paste the actual form code since the values are generated by a javascript calendar, but I will paste the code where the values are retrieved and the weird problem is found.

The $_POST values are properly sent and are correctly viewed in firebug console, with the right names/values.

echo $_POST['ordersDateFrom'];
	if(isset($_POST['ordersDateFrom']) && isset($_POST['ordersDateTo'])){
		echo $_POST['ordersDateFrom'];
		$_SESSION['ordersDateFrom'] = $_POST['ordersDateFrom'];
		$_SESSION['ordersDateTo'] = $_POST['ordersDateTo'];
		$results = $mysqli->query("SELECT * FROM orders WHERE (date BETWEEN '{$_POST['ordersDateFrom']}' AND '{$_POST['ordersDateTo']}')");
	}
	else{
		echo $_POST['ordersDateFrom'];
		$today = date("Y-m-d");
		$results = $mysqli->query("SELECT * FROM orders WHERE date = '$today'");
	}

Ok so, the problem is that none of the echo of the $_POST variables are outputted, and the query is always the one with $today so its obvious the variables are not recognized in the If statement, but.....both the $_SESSION variables update with the $_POST values!! This just makes no sense. Any help appreciated.

Link to comment
Share on other sites

Try putting this on the page to see exactly what is being sent to the page.

 

echo "<pre>" . print_r($_POST, TRUE) . "</pre>";

 

Although you think it is not needed, you should post the actual form code. It could be that the fields are not within the form, there are two forms on the page, or any number of things with the HTML code.

Link to comment
Share on other sites

without having all the code that REPRODUCES the problem all we can do is make guesses.

 

some possibilities -

 

1) output buffering is on in the php.ini and any output you are sending from the php code is being discarded because you are doing a header(), session_start(), or setcookie() statement after the code you have posted.

 

2) your page is being requested twice, perhaps once by javascript and a second time by the browser, and the result you are seeing is from the second page request.

 

if you care to post enough of your code that reproduces the problem, less any database credentials, someone could actually find what's causing the problem.

 

edit: also, since you are determining what will be displayed on the page, you should be making a get request, not a post request.

Edited by mac_gyver
Link to comment
Share on other sites

I have tried that, the array results empty. As I wrote before through the firebug console the $_POST variable are correctly seen and posted. The strangest part of all this is that the $_session variables are updated with the $_POST values o.O

 

I am double checking if any of the statements mentioned above are called after the form is sent. I'll post more code asap.

 

here is the code with the form 

<div style = 'width: 200px;'>
	<form method = 'POST'>
		<?php echo $xml->from ?>: <script>DateInput('ordersDateFrom', true, 'YYYY-MM-DD')</script>
		<?php echo $xml->to ?>: <script>DateInput('ordersDateTo', true, 'YYYY-MM-DD')</script>
		<input type = 'submit' value = '<?php echo $xml->submit; ?>'>
	</form>
</div>
<b><?php echo $xml->search_name; ?></b> <input type = 'text' onkeyup = 'filter("orders", value)'>
<span id = 'searchResults'>
<table class = 'results'>
	<tr>
		<th><?php echo $xml->date; ?></th>
		<th><?php echo $xml->name; ?></th>
		<th>TOT <?php echo $xml->buyPrice; ?></th>
		<th>TOT <?php echo $xml->sellvalue; ?></th>
	</tr>
	<?php
	$totalAll = 0;
	$qtyAll = 0;
	echo "<pre>" . print_r($_POST, TRUE) . "</pre>";
	echo $_POST['ordersDateFrom'];
	if(isset($_POST['ordersDateFrom']) && isset($_POST['ordersDateTo'])){
		echo $_POST['ordersDateFrom'];
		$_SESSION['ordersDateFrom'] = $_POST['ordersDateFrom'];
		$_SESSION['ordersDateTo'] = $_POST['ordersDateTo'];
		$results = $mysqli->query("SELECT * FROM orders WHERE (date BETWEEN '{$_POST['ordersDateFrom']}' AND '{$_POST['ordersDateTo']}')");
	}
	else{
		echo $_POST['ordersDateFrom'];
		$today = date("Y-m-d");
		$results = $mysqli->query("SELECT * FROM orders WHERE date = '$today'");
	}
Edited by 4E20
Link to comment
Share on other sites

So, the page is posting to itself? Where, exactly on the page, are the echos that you state are not working? If those echos are within certain types of HTML tags (or outside HTML tags), or happening before the BODY tag, there's no telling what you may "see" on the page. Have you checked the HTML source of the page to see if the expected values are there?

Edited by Psycho
Link to comment
Share on other sites

Yes the page posts to itself, the echoes I am talking about are the ones echoing the $_POST['ordersDateFrom'] variable. If i try to output any string in those echo they show correctly. The  $_SESSION['ordersDateFrom'] = $_POST['ordersDateFrom'];   and   $_SESSION['ordersDateTo'] = $_POST['ordersDateTo']; is working so I have no idea why the $_POST variable are not recognized in the echo, nor in a possbile print_r($_POST, TRUE) statement (which i tried to include and it returns empty array). That just makes no sense.

when I echo  $_SESSION['ordersDateFrom'] at the end of the page the new updated value is outputted!!

 

full page code:

<div class = 'boxHist'>
<?php
		$itemsAddedAll = 0;
		$amountAll = 0;
		$results = $mysqli->query("SELECT * FROM orders");
		while($row = $results->fetch_assoc()){
			$itemsAddedAll += $row['qty'];
			$costAll += $row['qty'] * $row['buyPrice'];
			$amountAll += $row['qty'] * $row['price'];
		}
	?>
	<table class = 'summary'>
		<tr>
			<td><h3><?php echo $xml->total_items_added; ?></h3></td>
			<th><h2><?php echo $itemsAddedAll; ?></h2></th>
			<td><h3><?php echo $xml->total_cost; ?></h3></td>
			<th><h2>€ <?php echo number_format((float)$costAll, 2, ',', '.'); ?></h2></th>
			<td><h3><?php echo $xml->total_amount; ?></h3></td>
			<th><h2>€ <?php echo number_format((float)$amountAll, 2, ',', '.'); ?></h2></th>
		</tr>
	</table>
</div>
<div style = 'width: 200px;'>
	<form method = 'POST'>
		<?php echo $xml->from ?>: <script>DateInput('ordersDateFrom', true, 'YYYY-MM-DD')</script>
		<?php echo $xml->to ?>: <script>DateInput('ordersDateTo', true, 'YYYY-MM-DD')</script>
		<input type = 'submit' value = '<?php echo $xml->submit; ?>'>
	</form>
</div>
<b><?php echo $xml->search_name; ?></b> <input type = 'text' onkeyup = 'filter("orders", value)'>
<span id = 'searchResults'>
<table class = 'results'>
	<tr>
		<th><?php echo $xml->date; ?></th>
		<th><?php echo $xml->name; ?></th>
		<th>TOT <?php echo $xml->buyPrice; ?></th>
		<th>TOT <?php echo $xml->sellvalue; ?></th>
	</tr>
	<?php
	$totalAll = 0;
	$qtyAll = 0;
	echo $_POST['ordersDateFrom'];
	if(isset($_POST['ordersDateFrom']) && isset($_POST['ordersDateTo'])){
		echo $_POST['ordersDateFrom'];
		$_SESSION['ordersDateFrom'] = $_POST['ordersDateFrom'];
		$_SESSION['ordersDateTo'] = $_POST['ordersDateTo'];
		$results = $mysqli->query("SELECT * FROM orders WHERE (date BETWEEN '{$_POST['ordersDateFrom']}' AND '{$_POST['ordersDateTo']}')");
	}
	else{
		echo $_POST['ordersDateFrom'];
		$today = date("Y-m-d");
		$results = $mysqli->query("SELECT * FROM orders WHERE date = '$today'");
	}
	
	while($row = $results->fetch_assoc()){
		echo "<tr class='borderx'>";
		echo "<td align='center'>{$row['date']} [{$row['time']}]</td>";
		echo "<td align='center'><font color='blue'>".fetchRecord('item', 'name', $row['itemID'], $mysqli)." X ".$row['qty']."</font><font color='purple'> - ".$xml->buyPrice." €".$row['buyPrice']."</font><font color='green'> - ".$xml->sellvalue." €".$row['price']."</font></td>";
		$total = $row['qty'] * $row['price'];
		$totalCost = $row['qty'] * $row['buyPrice'];
		echo "<td  align='center'>€ ".number_format($totalCost, '2', ',', '.')."</td>";
		echo "<td  align='center'>€ ".number_format($total, '2', ',', '.')."</td>";
		echo "</tr>";
		
		$totalAll += $total;
		$costTotalAll += $totalCost;
		$qtyAll += $row['qty'];
	}
	echo "<tr>";
	echo "<th colspan = '2'>".$xml->total_items_added." = $qtyAll</th>";
	echo "<th>€ ".number_format($costTotalAll, '2', ',', '.')."</th>";
	echo "<th>€ ".number_format($totalAll, '2', ',', '.')."</th>";
	echo "</tr>";
	?>
</table>
</span>
<?php if(isset($_SESSION['ordersDateFrom'])){echo $_SESSION['ordersDateFrom']." — ".$_SESSION['ordersDateTo'];} ?>
<!-- ACTIONS -->
<div id = 'popup'></div>
Edited by 4E20
Link to comment
Share on other sites

Is this your only form?  Are you sure your JavaScript is creating inputs?  Do they have names?  Consider adding a name to your submit input so you could check that.

        <form method = 'POST'>
               <?php echo $xml->from ?>: <script>DateInput('ordersDateFrom', true, 'YYYY-MM-DD')</script>
               <?php echo $xml->to ?>: <script>DateInput('ordersDateTo', true, 'YYYY-MM-DD')</script>
               <input type = 'submit' value = '<?php echo $xml->submit; ?>'>
        </form>

I saw that your print_r you added per Psycho's advice was nested in a table.  Have you checked the HTML to make sure it wasn't just not being displayed.

 

Have you tried syslog to make sure you are not hitting the server multiple times (or not at all)?

 

Not your question, but I would recommend doing your PHP logic first and then your presentation.  While a template engine is not needed (and some will say not to use one at all and just use PHP), I like them and recommend Twig.

Link to comment
Share on other sites

This is the only form in the page, and as I wrote in first post, through firebug console I can see and confirm that the variable are correctly passed with correct name and value, infact the $_SESSION variables do get updated. 

Link to comment
Share on other sites

You state that the post above is the full page code, but I don't see any HTML/HEAD/BODY tags. Is there more that you failed to provide. The only thing I can think of at this point is that there is some JavaScript code that is perhaps submitting the POST values via an AJAX call - then the page refreshes. So, the values are getting passed via POST, but the processing and output of those values is not getting consumed by the browser. Then, whenever the page refreshes, the SELECT queries will display the results of the previously posted data.

 

These types of problems always seem to end up having a logical explanation.

Link to comment
Share on other sites

These types of problems always seem to end up having a logical explanation.

 

This is impossible!  There is no way this is happening, but it is!  It defies gravity!

 

Whenever I've thought I witnessed the impossible, I've always eventually stumbled upon some tiny detail which makes what I've witnessed exactly as it should be.

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.