I am currently creating an image gallery website, and somehow no matter what I do, the first image that I posted on the system is always unavailable. I have this 'show info' button on the image, which does a post request that calls on the showinfo.php.
This is the display.php that displays all available image in the database.
if ($method == "0") {
$stmt = $conn->prepare("SELECT * FROM image");
$stmt->execute();
$result = $stmt->get_result();
echo "
<div class='container'>
<div class='row text-center' style='display:flex; flex-wrap: wrap;'>";
while ($row2 = $result->fetch_assoc()) {
echo "<div class='col-md-3 col-sm-4'>
<div class='thumbnail'>
<img src='".$target_dir.$row2['filename']."'>
<div class='caption'>
<h4>".$row2['name']."</h4>
</div>
<p>
<form action='showinfo.php' method='POST' enctype='multipart/form-data'>
<input type='hidden' name='image_id' value='".$row2['id']."'>
<button name='showinfo' class='btn btn-primary'>More Info</button>
</form>
</p>
</div>
</div>";
}
echo "</div> </div>";
}
This is how it looks like on the webpage, notice that the first image/first row of the table in mysql will still be fetched:
And when click on the 'more info' button, it will redirect to the showinfo.php, which shows something like this:
However, it never works for the first image. I have done some testings, it never gets to the showinfo.php page and it will always redirect back to the homepage after I click the button. I have used postman to do a post request, setting the image_id to 1 and that gets me the info page of the first image, just not when the button is clicked. I am using XAMPP 5.6.37
This is the expected result I should get for the first image
Ignore the jammed up stuff.. but the post request goes through.
This is the showinfo.php code
Why using form to show data of that product on next page what if user copy link and share it to somewhere else it will not have it's post data in it and result will be error page with no data. So far better way to use it.
if ($method == "0") {
$stmt = $conn->prepare("SELECT * FROM image");
$stmt->execute();
$result = $stmt->get_result();
echo "
<div class='container'>
<div class='row text-center' style='display:flex; flex-wrap: wrap;'>";
while ($row2 = $result->fetch_assoc()) {
echo "<div class='col-md-3 col-sm-4'>
<div class='thumbnail'>
<img src='".$target_dir.$row2['filename']."'>
<div class='caption'>
<h4>".$row2['name']."</h4>
</div>
<p>
<a href='showinfo.php?image_id=".$row2['id']."'> <button name='showinfo' class='btn btn-primary'>More Info</button></a>
</p>
</div>
</div>";
}
echo "</div> </div>";
}
on showinfo.php page you can use $_GET['image_id'] to get data from mysql as per image id and will work globally everywhere with link.
thanks for all the suggestions. Someone mentioned that I should try to take our the redirect code to see why the problem is happening. I did that, and I was able to get the first image's info without any issue. So I look into the code on my homepage ... It turns out it's none of the issues we talked about. On my homepage I have wrapped around the "method" selection code in a form, which is why it's causing that problem. My code originally look like this, and the showImages(this.value) is a js function that will call the display.php shown above.
The div with the id "showimage" is where all the thumbnails show up.
Once I take out the , I was able to solve my issue.
<form>
<div class='form-inline'>
<input class='form-control' type='text' id='menu_search_input' size='50' placeholder="enter content first if searching by name..">
<select class='form-control' id='menu_search' onchange='showImages(this.value)'>
<option value=''>Search by</option>
<option value='0'>Search by Latest</option>
<option value='1'>Search by Image Name</option>
<option value='2'>Search by Category</option>
<option value='3'>Search by Most Popular</option>
<option value='4'>Search by Least Popular</option>
<option value='5'>Search by Most Credit Needs</option>
<option value='6'>Search by Least Credit Needs</option>
</select>
<div id="showimage">Loading images ...</div>
</div>
</form>
Related
I am trying to create a form when the user clicks a button on a selected element. However, when i try it, i click a button and it either loads multiple, or loads 1 but shows only one value, if that makes sense. Here is my code:
<?php
$sql = "SELECT * FROM services";
$result = $database->query($sql);
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
?>
<div class="service_display">
<div class="service_header"><?php echo $row['service_name']; ?></div>
<div class="service_desc"><?php echo $row['service_desc']; ?></div>
<div class="service_options">
<button type="button" class="additional_files" id="additional_files">Edit</button>
</div>
</div>
<?php
}
?>
I have a form that will appear with jQuery when the button is pressed, and im trying to fill the values with the respective id's. Here is my code for that:
<div class="show-onclick">
<h3>Edditing service: <?php echo $row['service_name']; ?></h3>
<hr>
<form action="inc/save_edit.php?id=<?php echo $row['id']; ?>" class="service_editform">
<input type="text" name="service_name" value="<?php echo $row['service_name']; ?>">
<textarea><?php echo $row['service_desc']; ?></textarea>
</form>
</div>
I think the problem is me not knowing where to put that code, if i put it in the while loop, it works, but understandably, i have like 10 input fields pop up..
if i put it outside the while loop and run a separate query to fill it, its empty..
As per you explanation of the problem its clear that you are looping the forms along with the front end display divs, which is not that good idea.
Always its better to use one modal window and then depending upon the click you have to load the data onto it and then pop it up.
Anyway for your scenario there may be more than one solution but this one will also serve the purpose:
Put the "show-onclick" whole div inside the "service_display" div so that now also it gets looped with no issues.
Now when the button gets clicked find the closest "service_display" div, it will give you the dom element & then use that and popup the "show-onclick" div enclosed within that.
Follow the code below:
<?php
$database = new mysqli("localhost", "root", "", "so");
$sql = "SELECT * FROM services";
$result = $database->query($sql);
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
?>
<div class="service_display" id="sd_<?php echo $row['id']; ?>">
<div class="service_header"><?php echo $row['service_name']; ?></div>
<div class="service_desc"><?php echo $row['service_desc']; ?></div>
<div class="service_options">
<button type="button" class="additional_files" id="additional_files">Edit</button>
</div>
<div class="show-onclick">
<h3>Edditing service: <?php echo $row['service_name']; ?></h3>
<hr>
<form action="inc/save_edit.php?id=<?php echo $row['id']; ?>" class="service_editform">
<input type="text" name="service_name" value="<?php echo $row['service_name']; ?>">
<textarea><?php echo $row['service_desc']; ?></textarea>
</form>
</div>
</div>
<?php
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(".show-onclick").css("display", "none");
$(document).on("click", ".additional_files", function() {
$(".show-onclick").css("display", "none");
var sd_id=$(this).closest('div.service_display').attr("id");
$("#"+sd_id+" .show-onclick").css("display", "block");
});
});
</script>
The only change i made to your code is i have added a dynamic id field to service_display div.
And i have used jquery to show and hide the forms of respective clicked buttons.
Again here i have used oop based mysqli which is not mandatory yours is also perfectly fine.
So i'm trying to make it so when I select something from the first select box will create a PHP variable.
Say in the select box I select option 1, it will create a php variable like $number = 1;. or if I select option 5 it'll create a variable like $number = 5;.
I know this require's ajax, but I have minimal to no experience with this at all.
Code:
<form class="form-horizontal row" id="select-service">
<div class="form-group">
<label class="col-sm-4 control-label">Select Social Media</label>
<div class="col-sm-4">
<select class="form-control" name="category" id="category" onchange="func(this.value)">
<?php
$stmt = $pdo->prepare('SELECT * FROM categories');
$stmt->execute();
if($stmt->rowCount() > 0) {
echo'<option selected="true" style="display:none;">Select a social media</option>';
} else {
echo '<option selected="true" style="display:none;">No social medias are available</option>';
}
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Select Service</label>
<div class="col-sm-4">
<select class="form-control" id="service" onchange="quantity(this.value)">
<option selected="true" style="display:none;">Please select a category.</option>
</select>
</div>
</div>
<ul class="pager wizard">
<div class="pull-right">
<li class="next">
<button href="#" class="btn btn-info"><i>Next</i></button>
</li>
</div>
</ul>
You cannot alter PHP variables on runtime (at least not from the same document you are executing the AJAX script). This is because PHP and jQuery are executed on diferent levels:
PHP is server side processing and jQuery happens on the client side.
Yes. AJAX can be used to make PHP requests but it does not exactly can alter variables in real time.
AJAX is a client side solution for calling external php files. in these files you could do basically anything you can do with a php request:
Read data from server
Insert data to server
Update parameters
Etcetera.
What you could do, depending on what you need is desing your site so what you need could be done using an external php file.
You call myfile.php?number=1 using AJAX
And then in myfile.php
<?php
$number = $_GET["number"]; // this sets the $number var to 1
//process or do whatever you need with the number
echo $number;
?>
This echo call will not be displayed on sceen as it normally is but instead you fetch it to Javascript or jQuery and process whatever you need it for.
I recomend you read the W3 Schools guide on AJAX for JS because it contains examples and it is very well explained on what you can do and how tho acomplish it.
This code for getting order_no when click on link, I can't implement any javascript on this page because its work like template and its not the page appear in browser, any way its working fine but if i click on the link and open modal it shows the same order_no, I have to refresh page or open in new tab to get order_no.
sorry for my english, wish you got what i mean
<td align='center' span style="font-weight:bold;"><?php echo $f_customer['order_no']?></td>
edit.php
<?php
require_once 'config.php';
$q_edit_student = $conn->query("SELECT * FROM `orders` WHERE `order_no` = '$_REQUEST[id]'") or die(mysqli_error());
$f_edit_student = $q_edit_student->fetch_array();
?>
<div class = "modal-content ">
<div class = "modal-body">
<div class="form-group col-xs-4 col-md-4">
<label for="name" class="control-label">Order Number</label>
<input type = "text" name = "order_no" id="order_no" value = "<?php echo $_REQUEST['id']?>"tabindex="1" class="form-control" autofocus />
</div>
From what little i am understanding from the question. I am guessing that you are trying to show an edit form for a particular order
Try the following
HTML
<a class="edit_link" href ="edit.php?id=<?php echo $f_customer['order_no']?>" data-toggle = "modal" data-target = "#action"><?php echo $f_customer['order_no']?></a>
Javascript
$(".edit_link").click(function(){
var href = $(this).attr('href');
$.get(href).done(function(data){
$("#action>.modal-body").html(data);
});
});
some thing like this will do it for you.
I have a very simple form:
<form action="system/web/push.php" method="post">
<div class="row">
<div class="col card_input_push card_input_push_language">
<select name="<?php echo PARAM_PUSH_LANGUAGE; ?>">
<?php echo get_languages($mysqli); ?>
</select>
</div>
</div>
<div class="row">
<div class="col card_input card_input_push">
<input type="text" name="<?php echo PARAM_PUSH_TITLE; ?>" placeholder="<?php echo $language_array[LV_PUSH_INPUT_TITLE]; ?>"<?php echo set_value(PARAM_PUSH_TITLE); ?>required/>
</div>
</div>
<div class="row">
<div class="col card_input_push card_input_push_message">
<textarea name="<?php echo PARAM_PUSH_MESSAGE; ?>" placeholder="<?php echo $language_array[LV_PUSH_INPUT_MESSAGE]; ?>"<?php echo set_value(PARAM_PUSH_MESSAGE); ?>required></textarea>
</div>
</div>
<div class="row">
<div class="col text-center card_input_push card_button_push_send">
<button class="btn btn-default" type="submit" name="<?php echo REQUEST_SEND_GCM; ?>" value="<?php echo $language_array[LV_PUSH_INPUT_SEND]; ?>"><?php echo $language_array[LV_PUSH_INPUT_SEND]; ?></button>
</div>
</div>
</form>
When i now press the submit button i dont want the post to perform instantly. I first want a confirmation dialog to show up with yes or no answers and only if the user says yes i want the post action to be performed.
What i NOT want is to use JavaScript. Is there a way to do that?
EDIT
Thank's for the answers. To satisfy all respondents i would like to tell you, why i dont want to use JavaScript. The answer is simple. I have no idea about JavaScript. I never have used it before but the project im working on (as always) has to be finished ASAP. That's the only reason.
Yes there is a way, use GET (not POST) the first time, on the PHP file check if GET was used and if so generate a form that sends confirmation form back to the user. Then make confirmation form to submit using POST and you are done.
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// generate confirmation form
}elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
// process your data
}
If anyone has access to a book called Head First PHP-MySQL they cover a similar example on page 280.
Maybe you could do something like this, with some css for design your dialog.
change your form with :
<form action="this_page">
If you submit, you call this page, display the dialog with a form, and when you submit with second form, you post to your needed php file.
if (isset($_POST['your_submit'] )) {
echo "<yourdialog>";
echo "<form action="system/web/push.php">";
echo "content";
echo "<submit yes>";
echo "</form>";
echo "</yourdialog>";
}
Your workflow is:
gather form data
send to server
get confirmation that that's what the user wants.
if yes then process data.
If you want to do that all via php then the server's handling the full load, so you'll want two forms, one to collect and one to confirm. And you'll want to store the first form's data so you can process it according to the second form's result. A bit old fashioned but not difficult.
I have two php page.I'm using Bootstrap, php and mysql. In the first page I load three objects into div from mysql database of the user logged. To do this I'm using the next code:
<div class="container">
<div class="row">
<?php
require_once('function.php');
conectar('localhost', 'root', '', 'mydb');
$consulta1 = mysql_query('SELECT id FROM user WHERE username="'.$_SESSION["name"].'"');
$result = mysql_query('SELECT * FROM finc WHERE Usuario_idUsuario="'.$_SESSION["idUser"].'"');
if ($row = mysql_fetch_array($result)){
do{
echo '<div class="col-lg-4">' ;
echo '<img class="center-block img-circle" src="data:image/gif;base64,R0lGODlhOw=="
alt="Generic placeholder image" style="width: 140px; height: 140px;">';
echo '<h2 class="text-center">'.$row['name'].'</h2>';
echo '<p align="center">'.$row['data'].'</p>';
echo '<p align="center">'.$row['tao'].'</p>';
echo '
<a type="button" class="btn btn-success" href="secondPage.php" role="button">Entrar »</a>
';
echo'</div>';
}while ($row = mysql_fetch_array($result));
} else {
echo "¡ No data for this user!";
<a}
?>
<!-- /.col-lg-4 -->
</div>
<!-- /.row -->
</div>
I need send the id value depending of the button clicked for load the data associated in the next php page. For example, If I click in the second button created in the do-while loop I need send the id=2 to the sencondPage.php. I have searched how to do this, but only find how to send var into url like sencondPage.php?var=2, And I hate this because user can change url... And adding value into session, but on click I haven't get this.
So, how can I pass the corresponding id value when user click in the link??
Thanks!
You could use an html <form>:
<form method="post" action="[URL FOR NEXT PAGE]">
...
<input type="submit" name="value1" value="Button 1" />
<input type="submit" name="value2" value="Button 2" />
</form>
Now if someone clicks the first button it will send them to the next page with the post data: value1=Button%201, and if they click the second button the it will instead be value2=Button%202. In either case any other form elements inside the form will also be submitted via post. With PHP you can retrieve these values using something like:
if ($_POST['value1']) {
...
elseif ($_POST['value2']) {
...
}