I created an HTML table with PHP as follows:
$i = 1;
while($row = mysql_fetch_array($result)){
Print "<tr>";
Print "<td><input id='prEff[$i]' type='number' step='0.01' min='0.01' max='0.99' value='.85' </td>";
Print "<td>".$row['ErdEfficiency'] . "</td>";
Print "<td>".$row['TheoreticalSEC'] . "</td></tr>";
$i++;
}
The input cell has a jquery.change function that is called when the number is changed within the range. During debugging, clicking the up or down button once (in chrome) causes the input to run through all values until the upper or lower limit is reached. So the function is called, but only after the whole range of values is run through.
If the page is refreshed without debugging, the jquery is called, but the button sticks since it is experiencing the bug as described.
Here is the simple jquery function:
$('[id^="prEff["]').change(function (){
var test = this;
alert("hi");
});
Its been a difficult bug to track, and I'm not sure what is causing it. It has occurred with an onchange attribute in the input, as well as a javascript eventListener, and jquery change. It may be the way the PHP is printing the input but I've tried a couple different ways without success.
It seems like you have an unclosed <input> tag in your code. Make sure you close it and you should be fine:
Print "<td><input id='prEff[$i]' type='number' step='0.01' min='0.01' max='0.99' value='.85' /> </td>";
Related
I have two checkbox forms on the same page. When they are both there, the second form will not work. I'm wondering if there is a way to have both on the same page with them both needing the $index of the input.
The input fields are similar to this (the other is unchecked):
echo "<input type='checkbox' name='check[]' value='$index' onChange='this.form.submit()' checked='checked'>";
echo "<input type='hidden' name='itemDELETE$index' value='$bookID'>";
This is the other
echo "<input type='checkbox' name='checkbox[]' value='$index' onChange='this.form.submit()'>";
echo "<input type='hidden' name='itemADD$index' value='$bookID'>";
And the data is stored like:
$value = $_POST["check"][0];
$toDELETE = $_POST["itemDELETE" . $value];
and
$value = $_POST["checkbox"][0];
$toADD = $_POST["itemADD" . $value];
I have tried renaming $value but the issue lies in the $index on the input fields.
They are outputting correctly, but the second one will not store the value properly in it's variable, whereas the first does. The second is the delete one.
When I remove the first forms functionality, the second form works. But when I add it back, the second one stops working. Not picking up on the itemDELETE $value
If I manually put the value in like this "itemDELETE0" instead of ["itemDELETE" . $value]it works. But I need the $value to picked up on since it's a foreach loop.
My solution to this was to put the second form in different shortcode, and include it on the page. It works well and doesn't clash. Same code, just in a different shortcode.
Then on the page created, I used the shortcode for the first form and then put the shortcode for the second form below it.
And instead of defaulting it to checked, I had to use it unchecked. But that works alright.
The issue I have been experiencing deals with input from HTML which was generated using PHP echo statements. Here is the function I have that outputs the form:
function confirm_recipients()
{
echo "<form action = ' ' name ='email_options' method='post'>";
echo "<input type='submit' name='sendRecipients' value='Yes, I want to send the emails' >";
echo "</form>";
}
Later on in the same PHP page, I call the function and then check to see if the submit button was set.
confirm_recipients();
if (isset($_POST['sendRecipients']))
{
//perform actions
}
However, this code is not functional seeing as even when the submit button is set (clicked by the user), the if statement block is never executed. Perhaps there is an issue with posting from the same file I want to "read in" from? Thanks for any advice.
Updates
Thank you for such immediate response. Sadly none of the suggestions have worked (removing the space in the action value or the suggestion made by user623952). No errors have been reported, the button is just failing to be set. I am looking for other places in the file that might have errors, perhaps in the order I call the function.
This works fine for me:
<?php
print "<pre>".print_r($_POST,true)."</pre>";
confirm_recipients();
function confirm_recipients() {
echo "<form action = ' ' name ='email_options' method='post'>";
echo "<input type='submit' name='sendRecipients' value='Yes, I want to send the emails' >";
echo "</form>";
}
if (isset($_POST['sendRecipients']))
{
print "<br/>sendRecipients is set!<br/>";
}
?>
I think your problem might be somewhere else in the code.
It's okay to POST the form data to the same script that contains the form. Change the action attribute to the URL of the script, do not set it to whitespace, which is what you did.
I don't think the value of a submit input is sent as part of the POST. Try using an input type="hidden" with the name 'sendRecipients'.
I am trying to build a table using PHP and SQL values. One of those table columns is going to have a numerical input that controls other values in that row. This parameter ranges from 0 to 1 and I'm trying to use Javascript to dynamically update the three columns to the right of it depending on the parameter value.
Here is the PHP commands for the html table:
$i = 1;
while($row = mysql_fetch_array($result)){
Print "<tr>";
Print "<td><input id='toChange[$i]' name='toChange[$i]' type='number' step='0.01'
min='0.01' max='0.99' value='.85' </td>";
Print "<td>".$row['dependentOnParameter1'] . "</td>";
Print "<td>".$row['dependentOnParameter2'] . "</td></tr>";
$i++;
}
I have tried many different tags for the input cell, such as toChange[] and toChange$i (this one as if the number grows with the row number). This table prints correctly, and here is the jQuery function I have tried to implement:
$('#prEff[1]').change(function() {
var test = this;
return;
});
I know how to alter the table once I get this function to correctly call, but the use of an input inside an array seems to be causing trouble. Is there a specific jQuery object or tag that must be used for .change for arrays? Or if I can use the table tag, will the this pointer point to the part of the table that has been altered?
You probably need to escape the brackets if they are part of the selector:
$('#prEff\\[1\\]')
I need a small hint. Let's say there's a code like:
<?php
$test = mysql_query("SELECT * FROM test WHERE id='1'");
while($row = mysql_fetch_array($test))
{
echo "<tr>";
echo "<td>" . $row['platform_name'] . "</td>";
echo "<td>" . $row['scan_frequency'] . "</td>";
echo "<td><input id='freq' type='text' /></td>";
echo "<td><input id='date1' class='datepicker' type='text' /></td>";
echo "<td><input id='date2' class='datepicker' type='text' /></td>";
echo "<td><button id='save'>Save</button></td>";
echo "</tr>";
}
?>
The logic is that when I run a page, I get platform name and scan frequency fetched from table test as supposed to. Then I need to fill out three inputs (new frequency and dates, which are in format yy-mm-dd) and send it back (update/set) using 'save' button to table test. How can I do it with AJAX/PHP?
Thanks in advice!
Here's a small hint. ;-)
You link the button attribute onclick to a javascript function (e.g. fetch_new_data). Then you define the function fetch_new_data, which sends an ajax request to the server. The server processes the ajax request, selects the new data, packages it as xml or json and sends it back to the client. On return of the data, you lookup the position of the table, where you want to insert the new data and extend the table with a few new rows.
You can look for jquery, which simplifies several of these tasks on the client side. At the server side, for PHP and JSON, see PHP - JavaScript Object Notation and any tutorial or example covering PHP+JSON and/or PHP+AJAX.
I have printed the table get from database like below
And my code
print "<form id='form1' name='form1' method='post' action='http://localhost/live/index.php?m=sync&a=update'>";
while($db_field = mysql_fetch_assoc($result))
{
print "<table border=1 width=100%>";
print "<tr><td width=5%><input name='taskid' type='text' value=".$db_field['task_id']." readonly=readonly></td>";
/*print "<td width=10%>".$db_field['task_name']."</td>";*/
print "<td width=5%><input name='percent' type='text' value=".$db_field['task_percent_complete']." readonly=readonly></td>";
print "</table>";
}
print "<input name='Sync' type='submit' value='Sync' id='Sync' />";
print "</form>";
mysql_close($db_handle); /*.$db_handle */ /*<---to check the resource link and id*/
}
else{
print"Failed" ;//.$db_handle;
So what i want to know how to parsing all the datas from the database to another page like the action i want to send, because now i just can parsing 1 data in 1 table when i click the sync button like below is my parsing code in another page
The image below is the parsing code
I have no idea how to display all the data in new page...it is need to use loop? im newbie, big thanks
Edit based on comments below:
For starters, in the first code, move the print "<table border=1 width=100%>"; and print "</table>"; parts outside the while loop because the table is getting generated twice as two separate tables.
What's happening with the current code is that the second pair of fields taskid and percent are overwriting the first because the names are the same, so you only see the second pair of values in your screenshot. You need to make the input field names an array: put square brackets [] in the name so that taskid and percent are automatically arrays in $_POST. This is what the HTML source should look like in the form:
<input name='taskid[]' type='text' ...>
<input name='percent[]' type='text' ...>
(Or manually subscript them like taskid_0, percent_0 and taskid_1, percent_1; but then you have to decide when to stop looping or use variable variables.)
After that, in the target page where you are processing $_POST["taskid"], it will be an array which you can access as $_POST["taskid"][0], $_POST["percent"][0] and $_POST["taskid"][1], $_POST["percent"][1]. So loop through that when updating the database and generating your 2nd html:
print_r($_POST["taskid"]); // verify that this is an array
print_r($_POST["percent"]); // also an array
$i=0;
foreach ($_POST["taskid"] as $t) {
// not using $t, that's just for looping convenience
// do sql insert/update here, i'm just showing how you'd loop the array
print "<td><input type='text' name='".$_POST["taskid"][$i]."' readonly=readonly></td>";
print "<td><input type='text' name='".$_POST["percent"][$i]."' readonly=readonly></td>";
$i++;
}
Hope that helps.
Some pointers: 1. Don't put images of your code, just paste the code. 2. The generated HTML copy-pasted (browser -> view source) is more useful than an image of the page.
version 1:
1. If I understand it correctly, you want to show the form on one page and when the user clicks Sync (sumbit) you want it to go to another page which will process the form?
Use the target attribute of the <form> element (link). Like:
<form action="form_action.php" method="post">
Or,
2. If you want to process the form first (same page) and send the user to another page:
send an HTTP 302 re-direct + Location header.
You can also set the redirect in your HTML after processing the page using:
<meta http-equiv="Refresh" content="0; url=http://www.example.com/" />