AJAX form sending data, PHP form not receiving it - php

I had this working earlier, but now it doesn't seem to want to pick up the data. It's possible something changed when I was trying to make it stop refreshing the page.
The form submits, but only for certain links. However the data gets sent across as normal. It's extremely strange.
This is the page the data is being sent to. Right now it just accepts the data and posts it for testing. "Text" shows but nothing else.
<?php
$ship_id = $_POST['transfer_ship'];
$char_id = $_POST['transfer_character'];
$pos_id = $_POST['transfer_position'];
$requested_by = $_POST['transfer_player'];
echo 'Success';
echo $ship_id;
echo $char_id;
echo $requested_by;
echo $pos_id;
echo $char_owner;
echo 'Text';
This is the form it's being sent from. It's part of a PHP UL series that's running. For the first result, the jQuery fires, I get the "Success" alert. For any of the other li links the data apparently gets sent (I use the F12 dev tools in Chrome, under Network, to see the header) but the page above either doesn't receive it or doesn't do anything with it.
jQuery:
<script type="text/javascript" src="../fancybox/jquery.fancybox.js?v=2.1.3"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".fancybox").fancybox();
});
</script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#transfer').ajaxForm(function() {
alert('Success!');
});
});
</script>
Form and UL:
<ul>
<?php
while(list($key, $val) = each ($arrayresult))
{
echo '<a href="#inline' .$val. '" class="fancybox"><li style="padding: 2px; margin: 0px; list-style: none;">';
echo '<img src="../images/profilepics/'.$charpic.'" style="float: right; padding-left: 10px; width: 40px;" />';
echo '<h2>'.$val.' Position</h2>';
echo '<p>Click here to apply for this position.</p>';
echo '</li></a>';
echo '<div id="inline' .$val. '" style="display:none;">';
echo '<h1>Request Character Transfer</h1>';
echo '<hr>';
echo '<form id="transfer" action="TransferRequest.php" method="post">';
echo '<label for="transfer_character">Character to Transfer</label>';
echo '<select id="transfer_character" name="transfer_character">';
echo '<option value="">Select Character</option>';
$request_character_query = "SELECT * FROM character_database WHERE character_active ='1' AND user_id = $user_id ORDER BY character_firstname DESC";
$request_character_result = mysqli_query($mysqli, $request_character_query);
/*** loop over the results ***/
foreach($request_character_result as $charrow)
{
/*** create the options ***/
echo '<option value="'.$charrow['character_id'].'">'. $charrow['character_firstname'] . " " . $charrow['character_surname'] . '</option>'."\n";
}
echo '</select>';
echo '<p>Applying for the '.$val.' position on the '.$shipname.'</p>';
echo '<p>If this is correct, please submit below</p>';
echo '<input type="hidden" value="'.$val.'" name="transfer_position">';
echo '<input type="hidden" value="'.$ship_id.'" name="transfer_ship">';
echo '<input type="hidden" value="'.$user_id.'" name="transfer_player">';
echo '<input value="Submit Request" type="submit" class="styled">';
echo '</form>';
echo '</div>';
}
?>
</ul>

I figured it out! I have my htaccess file removing the .php extension, but I had .php on the redirect link for ajax. So it was hitting the .php page then redirecting WITHOUT the POST data.

Related

Passing a variable in $_POST parameters not working in PHP?

I am displaying images from a database, each image has a comment box that contains a form and a submit button; the submit button has the name of the image that is displayed. Ex. if the image name is flowers.jpg, the name for the submit button is set to flowers.jpg. (I did string replace though, so in my code it would be set to flowersjpg) The names are added to the submit button with no problem at all. Because I have several images, I wanted to pass $row['image'] (image name) into $_POST[ ] parameters for an isset() function but it is not working. - All the code is in one document
$result = mysqli_query($conn, "SELECT * FROM uploads ORDER BY timestamp DESC");
while($row = mysqli_fetch_array($result))
{
// Prints all the photos with a caption
echo "<div class='wrapper' name=".$row['image'].">";
echo "<div class='caption'>";
echo "<h3>".$row['title']."</h3>";
echo "<p class='description'>".$row['description']."</p>";
echo "</div>";
echo "<img src='images/uploads/".$row['image']."'/>";
// name for submit button, the $row['image'] contains the entire image name so certain characters have to be removed
**$name = str_replace(array(".", "-", ":", "'", "/", " "), "", $row['image']);**
// comment box - just a form with a submit
echo "<div class='commentBox'>
<form action='test.php' method='POST'>
<textarea placeholder='Say something nice!' name='comment'></textarea>
// CREATES NAME FOR BUTTON. FOR EACH IMAGE, THE BUTTON HAS THE IMG NAME
**<button type='submit' name='".$name."' value='submit'> post </button>**
</form>
</div>";
echo "</div>";
}
if(isset($_POST["'".$name."'"]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
When I put the name as a plain string in the $_POST parameters, it works fine but when I put the variable in, it does not work. The $name variable is a string.
This works but I'm sure any string used will work as long as the button name and string in $_POST are the same.
echo "<button type='submit' name='flowerspng' value='submit'> post </button>";
if(isset($_POST[flowerspng]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
This code on the other hand, does not. I know variables can be passed inside $_POST parameters, so I don't understand what's wrong with my code. Is my syntax incorrect? Clearly the variable is not being set otherwise the alert box would pop up.
echo "<button type='submit' name='".$name."' value='submit'> post </button>";
if(isset($_POST["'".$name."'"]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
I have tried this too but I don't think it worked because there are no quotes around $name.
echo "<button type='submit' name=$name value='submit'> post </button>"
if(isset($_POST[$name]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
This code works:
echo "<button type='submit' name='$name' value='submit'> post </button>"
if(isset($_POST[$name]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
I moved the if statement for the isset() function into the end of the while loop, it works for all images now.
while($row = mysqli_fetch_array($result))
{
echo "<div class='wrapper' name=".$row['image'].">";
echo "<div class='caption'>";
echo "<h3>".$row['title']."</h3>";
echo "<p class='description'>".$row['description']."</p>";
echo "</div>";
echo "<img src='images/uploads/".$row['image']."'/>";
$name = str_replace(array(".", "-", ":", "'", "/", " "), "", $row['image']);
echo "<div class='commentBox'>
<form action='test.php' style='text-align: right; background-color: rgb(34,34,34); width: 100%; height: 120%; margin: 0;' method='POST'>
<textarea style='height: 90%; width: 79%; resize: none; border-radius: 3px; margin: 0; margin-top: 5px;' placeholder='Say something nice!' name='comment'></textarea>
<button type='submit' name='$name' value='submit' style='height: 100%; width: 20%; float: right;'> post </button>
</form>
<div class='seeMore'> see more </div>
</div>";
echo "</br><div class='comments' style='display: none;'> comment display </div>";
echo "</div>";
**
// testing the if statement for each image - works perfectly now
if(isset($_POST[$name]))
{
echo '<script type="text/javascript"> alert("'.$name.'"); </script>';
mysqli_query($conn, "UPDATE uploads SET title='test' WHERE image='".$row['image']."'");
header("Refresh:0");
}
**
}

PHP, mulitchoice FORMS, how to pass variable selected in form,

and show form based on that variable in single page, without pressing SUBMIT or buttons?
i have loop for fill 'select' in FORM
echo '<select name="inne_zad" class="cfpa" width="400" style="width: 400px" >';
echo '<option value="">Nr Zadania | Typ Zadania | Nazwa Wioski</option>';
while($option = mysql_fetch_assoc($zadanie1)) {
echo '<option value="'.$option['id_zadania'].'">'.' '.$option['id_zadania'].' - '.$option['rodzaj_zadania'].' - '.$option['zad_cel_nazwa'].' '.'</option>';
}
echo '</select>';
and css rule
<style>
.cfp { display: none; }
.cfpa:checked + .cfp {
display: block;
}
</style>
and div outside form (inside FORM css display none - not working..
echo ' <div class="cfp">';
echo ' some other content';
if ($_POST['id_zadania'] == 7 ){ echo 'wybrales 7'; }
echo ' </div>';
I want to get selected variable, and display other part of the FORM
is it possible with PHP and CSS only?
If you don't want to press a button you can use the event onChange.
echo '<form>
<select name="inne_zad" class="cfpa" width="400" style="width: 400px" onChange="this.form.submit();" >';

Unique Value For Each Button

I have two php pages. The first page queries a database and places some data into a form
The second page, grabs the data submited from the previous page and displays the data.
THE ISSUE:
On the second page, the information is displayed in a table. At the end of each row, there is a "information" button that allows the user to click and see more details related to the data in that particular column. For some reason, the information button for every record in the table is holding the value of the first record returned in the database query. In other words no matter what button I click on in the table, it always relates to the first record returned in the query.
I'm hoping someone can help me find a solution to linking each "information" button to each unique record. In other words If I click the info button in row one, it will display the data for the record related to row 1...etc. Here's my code.
PAGE 1
<?php
$query = mysql_query ("SELECT * from cust_data group by cabinet_number ORDER by cabinet_number ASC");
WHILE ($rows = #mysql_fetch_array($query)) {
if (($rows['account_number']=="") &&
($rows['customer_first_name']=="") &&
($rows['customer_last_name']=="") &&
($rows['company_name']=="")) {
echo '<form method="GET" action="cabinet_result_page.php">
<input type="hidden" value="'.$rows['cabinet_number'].'" name="cabinet_number">
<input type="hidden" value="'.$rows['company_name'].'" name="company_name">
<img src="images/bulletpoint_green.png">
<input type="submit" value="'.$rows['cabinet_number'].'" name="'.$rows['cabinet_number'].'" id="submit">
</form>';
} else if ($rows['cabinet_number']!=="") {
echo '<form method="GET" action="cabinet_result_page.php">
<input type="hidden" value="'.$rows['cabinet_number'].'" name="cabinet_number">
<input type="hidden" value="'.$rows['company_name'].'" name="company_name">
<img src="images/bulletpoint_red.png">
<input type="submit" value="'.$rows['cabinet_number'].'" name="'.$rows['company_name'].'" id="submit">
</form>';
}
}
}
PAGE 2:
GRABS THE DATA IN PAGE ONE AND PLACES IT IN A TABLE. WHEN THE 'INFO' BUTTON IS CLICKED, MORE INFORMATION IS DISPLAYED IN A JQUERY POPUP
db_connect();
$cabinet_number = $_GET['cabinet_number'];
$company_name = $_GET['company_name'];
$query = #mysql_query ("SELECT * FROM cust_data WHERE cabinet_number = '$cabinet_number' ");
WHILE ($rows = #mysql_fetch_array($query)) {
echo'<tr>
<td><font size="4">'; echo $rows['account_number']; echo'</font></td>
<td><font size="4">'; echo $rows['customer_first_name']; echo '</font></td>
<td><font size="4">'; echo $rows['customer_last_name']; echo '</font></td>
<td><font size="4">'; echo $rows['company_name']; echo '</font></td>
<td><font size="4">'; echo $rows['cabinet_number']; echo'</font></td>
<td><font size="4">'; echo $rows['key_tag_number']; echo'</font></td>';
if ($rows['switch_and_port1'] =="") {
echo '';
} else if ($rows['switch_and_port1'] !== "") {
echo '<td><font size="4">';
echo '<input type = "image" src= "images/view_details.png" height="16" width="16" class="my_modal_open">', '</font></td>';
}
{
echo '<td><font size="4">'; echo '<input type = "image" src= "images/view_details.png" height="16" width="16" class="my_modal_open">', '</font></td>';
}
echo '</tr>';
echo '<div class="well" style="display:none;margin:1em;" class="my_modal">';
echo '<img src="images/hdc_logo_transparent.png"><br>';
echo '<div style="height:23px; width:100%; background-color:black"> <h4><font color="#FFFFFF">',' ', 'Cabinet: ', $id, '</font></h4></div>';
echo '<p>';
echo '<br>';
echo '<img src="images/bulletpoint_orange.png">';
echo 'Power: ', $rows['power_circuit'];
echo '<br>';
echo'<img src="images/bulletpoint_orange.png">';
echo 'Sw/Po: ', $rows['switch_and_port1'];
if ($rows['switch_and_port2'] =="") {
echo '';
} else if ($rows['switch_and_port2'] !== "") {
echo '<br>';
echo '<img src="images/bulletpoint_orange.png">';
echo 'Sw/Po: ', $rows['switch_and_port2'];
}
if ($rows['switch_and_port3'] =="") {
echo '';
} else if ($rows['switch_and_port3'] !== "") {
echo '<br>';
echo '<img src="images/bulletpoint_orange.png">';
echo 'Sw/Po: ', $rows['switch_and_port3'];
}
if ($rows['switch_and_port4'] =="") {
echo '';
} else if ($rows['switch_and_port4'] !== "") {
echo '<br>';
echo '<img src="images/bulletpoint_orange.png">';
echo 'Sw/Po: ', $rows['switch_and_port4'];
}
echo '</p>';
echo'</div></form>';
}
echo'<script>
$(document).ready(function() {
$(".my_modal_open").click(function(){
$(this).closest("tr").next(".my_modal").popup({"autoopen": true});
});
});
});
</script>
</body>
</html>';
}
The issue is that you are setting the same id attribute to all the modals (which is invalid HTML markup) and expecting the click handler on the button to figure out which one to open.
UPADTE
Another issue with your markup is that you can't have <div>s as siblings of a table row, it will produce unexpected results as browsers will attempt to fix the markup and rearrange the elements, you could place the divs inside a td or use a second loop to build them.
Now I also did some digging in your popup plugin and I must say it's not very well implemented, I would recommend you look for another one. That being said I did manage to get it working, here's what you need to do:
Set an unique id for each of the modal divs, if you have some sort of id in your database data you can use that, if not then you can set up a counter variable inside the loop:
echo '<div class="well" id="my_modal'.$rows['id'].'">';
Use the same id as the class for the button that should open the modal but add "_open" as a suffix:
echo '<input type = "image" src="images/view_details.png" height="16" width="16" class="my_modal'.rows['id'].'_open">
You can now initialize the popup plugin on your divs and it will automatically assign the open action to the corresponding button:
echo'<script>
$(document).ready(function () {
$("[id^=\'my_modal\']").each(function () {
$(this).popup();
});
});
</script>';
That should fix the modal problem, here's a working demo.
Now I also have a couple observations about your code:
You should avoid the use of inline styles and as I mentioned earlier the <font> tag should be replaced for proper CSS, I would also suggest using CSS padding/margin to add space between words instead of
This type of code:
if ($rows['switch_and_port1'] =="") {
echo '';
} else if ($rows['switch_and_port1'] !== "") {
echo 'SOME CONTENT';
}
can be optimized to just:
if ($rows['switch_and_port1'] !== "") {
echo 'SOME CONTENT';
}
echoing an empty string produces no output whatsoever and there's no need for an elseif

Login Button similar to dropbox using images

I am currently trying to create a login button that is similar to that of dropbox. There is another thread available on how to do this, but I wish to do it differently. Note I tried to follow the code provided in that thread as close as I can, but it was a failed attempt.
Rather than having a link written in text, my button refers to a link through an image. Furthermore, when the button is pressed, I want another image to popup right below it.
Here is my code:
html/ jquery /php code:
echo '<div id = popup>';
echo '';
echo '<div id = "popupimage"> </div>';
echo '</div>';
echo '<script type="text/javascript src="jquery.js">';
echo '$("#popup").click(function(e){
$("#popupimage").css("visibility","visible");
e.stopPropagation();
});';
echo '</script>';
css code:
#logbutton{
top:50px;
left:850px;
position: absolute;
background-image: url(../images/buttons/loginbutton.png);
width:59px;
height:28px;
}
#popupimage{
top:63px;
left:887px;
position: absolute;
background-image: url(../images/popupimage.png);
visibility: hidden;
width:400px;
height:600px;
}
If possible I would also like to know if this can be done using html 5 and css only?
Thanks in advance
I have managed to do this. Here is the code:
echo '';
echo '<div id = "popup">';
echo '<div id = "popupimage"> </div>';
//HTML INSIDE POPUP
echo '</div>';
echo '<script type="text/javascript" src="jquery.js"></script>';
echo '<script>';
echo '$("#loginbutton").click(function(e){';
echo '$("#popup").css("visibility","visible");';
echo 'e.stopPropagation();';
echo '});';
echo '$("#popup").click(function(e){';
echo 'e.stopPropagation();';
echo '});';
echo '$("body").click(function(e){';
echo '$("#popup") . css("visibility", "hidden");';
echo '});';

Performing different actions on dynamically generated buttons. PHP

I am working on a PHP Gallery application, and need some help here. Actually I have a page where images from a specific directory are displayed directly. With each one of the images displayed there is a dynamically generated submit button that will be used to delete respective images separately.
Every image has its own submit button, that will be used to delete that image. Being new to php I need some method that can be called to delete only that image from the actual or physical directory.
There is a similarity between image and button that I have coded it such that every image and its respective button has names such as "img_1" and its button is "del_1".
<form id="albumGallery" name="albumGallery" method="POST">
<?php
$dir = htmlspecialchars($_GET["p"]) . "/";
$imgs = array();
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (!is_dir($file) && preg_match("/\.(bmp|jpe?g|gif|png)$/", $file)) {
array_push($imgs, $file);
}
}
closedir($dh);
} else {
die('cannot open ' . $dir);
}
$i=0;
echo "<div id='images'>";
foreach ($imgs as $idx=>$img) {
//$class = ($idx == count($imgs) - 1 ? ' class="last"' : '');
echo '<table style="float: left; border: 1px solid #EFEFEF; border-radius: 5px; padding: 5px; margin: 5px;"><tr><td><a href="'. $dir . $img .'" rel="example_group" ><img src="' . $dir . $img . '" alt="' . $img . '" id="'. "img_" . $i .'"/>
</a></td></tr><tr><td><input type="submit" class="ctrlDelete" value="" id="'. "del_" . $i .'"/></td></tr></table>';
$i++;
}
echo "</div>";
?></form>
So, I need to make a method so that each button deletes its respective image and the form is posted back to self.
For your issue, it is better to use anchors. You can style them as pseudo-buttons, if you want. Then just generate links like delete.php?id=23, which will execute the appropriate deletion script with $_GET argument passed.
Below is the very simple implementation:
<table>
<tr>
<td>Title</td>
<td>Image</td>
<td>Actions</td>
<tr>
<?php
foreach ($table as $row)
{
echo "<tr>";
echo "<td>".$row['title']."</td>";
echo "<td>".$row['image']."</td>";
echo "<td>";
echo "<a href='delete.php?id=".$row['id']."'>Delete</a>";
echo "<a href='edit.php?id=".$row['id']."'>Edit</a>";
echo "</td>";
echo "</tr>";
}
?>
</table>
delete.php and edit.php should contain the following code at the very end:
<?php
header("Location: http://www.example.com/");
?>
#Edward Ruchevits
Thanks for your help :D,
I did not use the header(); method but used the javascript's settimeout(); to redirect my page. Here is my code...
<script type="text/javascript">
setTimeout("window.location = '<?php echo $_SERVER['HTTP_REFERER'] ?>'", 1);
</script>
<?php
$path = htmlspecialchars($_GET["p"]);
unlink($path);
?>
I suggest adding the form tag inside your foreach loop and post each of those forms to self. Each form can simply include a hidden field with the image ID. Then each time the page loads, you can simply check the $_POST variable for the image and delete that before serving up your page.
Alternately, you might consider using checkboxes next to the images - then one form and one submit button can action multiple deletions in one - far more efficient in my opinion.
Hope this helps!

Categories