I am trying to $_GET a hidden input using PHP.
When I check the html code in Chrome, Safari, etc., I can see the value of the hidden variable but when I try to echo it with PHP it is empty. All of this is inside a modal.
I don't really understand what I am doing wrong.
I hope some of you can help me.
<form method="get">
<input name="hiddencontainer" type="hidden" id="hiddencontainer" value="default"/>
</form>
<div id="frame" class="modal">
<div class="modal-content">
<!-- Body content -->
<div class="modal-body">
<p id="content">
<?php
// Get information
$id = $_GET["hiddencontainer"];
echo $id;
?>
</p>
</div>
</div>
</div>
You need to submit the form to get the value, try this
<form method="get">
<input name="hiddencontainer" type="hidden" id="hiddencontainer" value="default"/>
Click the button to submit the form and you will see the value
<input type="submit" name="Submit form"/>
</form>
<div id="frame" class="modal">
<div class="modal-content">
<!-- Body content -->
<div class="modal-body">
<p id="content">
<?php
// Get information
$id = $_GET["hiddencontainer"];
echo $id;
?>
</p>
</div>
</div>
</div>
Related
First I'm beginner in php, second I'm trying to trace the value of the hidden input which is " the ID of the image in table products ", but whenever I click the delete button of any image, it always gives me the last id of the last image in my products table , and when I changed the input into text it prints the correct id but if I used it with POST it will not work .
Here is the codes :
<?php
if(isset($_POST['delete'])){
$dataBase = mysql_connect("localhost","root","");
mysql_select_db('HouseOfCake');
$PID = $_POST['PID'];
echo $PID ;
}
?>
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<form method="POST" action="Delete.php">
<div class="container">
<?php
$dataBase = mysqli_connect("localhost","root","" , "HouseOfCake");
?>
<div class="row text-center">
<?php
$r=mysqli_query($dataBase,"SELECT*FROM Products");
while($Products=mysqli_fetch_array($r, MYSQLI_ASSOC)){
?>
<div class="col-lg-3 col-md-6 mb-4">
<div class="card" name= <?php $Products['CakeID']; ?>>
<image src = <?php echo
'data:image/jpg;base64,'.base64_encode($Products['Image']).'' ; ?> />
<div class="card-body">
<h4 class="card-title"> <?php echo $Products['Price']; ?> SR. </h4>
<p class="card-text"> Details.</p>
</div>
<div class="card-footer">
<input type="text" value= "<?php echo $Products['CakeID'] ?>" name="PID" >
<input type = "submit" name="delete" value=" Delete Item."
style="width:250px" >
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</form>
</body>
</html>
To clean up what you are doing, so its easier to understand, you have this in a nutshell:
<form>
while {
<inputs>
}
</form>
What this will do is submit ALL those inputs to PHP. And since each one of the inputs in the while have the same name, PHP will only use the 'last one received'.
You want to have your flow like this:
while {
<form>
<inputs>
</form>
}
That way each form only submits the inputs defined inside of it (not all of them on the page).
So to recap with your code, you would want to have multiple forms on the page as such:
<?php while($Products=mysqli_fetch_array($r, MYSQLI_ASSOC)){ ?>
<form method="POST" action="Delete.php">
<input type="text" value="<?php echo $Products['CakeID'];?>" name="PID">
<input type="submit" name="delete" value="Delete Item." style="width:250px">
</form>
<?php }?>
I'm learning now php and i'm stuck in one place with handling form submit action.
Im my input i'm trying to store user name in $_GET['firstname'] variable. But it's empty. I mean, that after checking if $_GET['firstname'] isset I get false. Where is my mistake?
<body>
<div class="container">
<div class="section back">
<form class="form myform" action="addcustomer.php" method="get">
<span class="myformtitle">
Add new User
</span>
<div class="form-group">
<div class="col validate-input">
<span class="label-input">Firstname</span>
<input id="firstname" class="input myinput" type="text" name="firstname" placeholder="Enter your firstname" value="<?php if (isset($_GET['firstname'])) echo $_GET['firstname']?>">
<span class="focus-input"></span>
</div>
</div>
<div class="col col-btn">
<button type="button" name="submit" class="btn sb-btn btn-block">
<span class="btn-sp">
Submit
</span>
</button>
</div>
</form>
</div>
</div>
<?php
if (isset($_GET['firstname'])) {
echo '<script>console.log("' . $_GET['firstname'] . '")</script>';
} else {
echo '<script>console.log("no name")</script>';
}
?>
</body>
Change your button type from button to submit.
button types are for Javascript (JS).
submit types are used to process PHP (form) directives.
My submit button suddenly stopped working. It submits data into a MySQL database.
While I was doing some design changes, it suddenly stopped working.
Are there any apparent errors/mistakes in the code below?
I'm a noob who's currently trying to learn some PHP and so on, so any help would be much appreciated. :)
<section id="moviesearch">
<!-- Section -->
<div class="subcribe2">
<div class="container">
<!-- Container -->
<div class="row">
<!-- Row -->
<div class="col-md-4">
</div>
<body ng-app="myApp">
<div ng-controller="MyCtrl" class="col-md-4">
<form class="form-watch-list-create">
<input required="required" placeholder="Movie title" type="text" class="form-control" typeahead="item.Title for item in getLocation($viewValue)" typeahead-loading="loadingLocations" typeahead-min-length="2" typeahead-wait-ms="1000" typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html" ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</body>
</div>
<!-- End Row -->
</div>
<!-- End Container -->
</div>
<script type="text/ng-template" id="customTemplate.html">
<a>
<span bind-html-unsafe="match.label | typeaheadHighlight:query" style="width: auto;"></span>
({{match.model.Year}})
<!-- <img ng-src="{{match.model.Poster}}" width="120"> -->
</a>
</script>
<div ng-controller="MyCtrl">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
</section>
Edit
Here's the code that sends data into the MYSQL database:
function addWatchList() {
if (isset($_GET['addtowatchlist'])) {
$name = $_GET['name'];
$conn = dbmysql();
$query ="INSERT INTO watch_list values(null,'{$name}','{$_SESSION['user_id']}','NOW()')";
if (mysqli_query($conn,$query)) {
$last = "SELECT * FROM watch_list WHERE created_by = '{$_SESSION['user_id']}' order by id desc limit 1";
$data = mysqli_query($conn,$last);
while ($row = mysqli_fetch_assoc($data)) {
include "_view.php";
}
}else {
echo "An error occurred. Please try again.";
}
exit;
}
}
I am looking to your code, missing attribute action="somefile.php" and method="get" if you are planning on submitting it using the form, you should put it or if you are planning on submitting your code using javascript you can use <form onsubmit="myFunction()"> That is what you are missing. I am not seeing you addtowatchlist name from your input so that php can catch it to your isset.
The submit button isn't inside the form.
You're missing the </form> tag to end the <form>. But you have </div> that matches the <div> just before </form>, so it's implicitly closing the <form> tag as well. Then you have <input type="submit"> after it. Since the submit button isn't inside the form, and has no form tag associating it with the form, it doesn't know which form it should submit when you click on it.
Try this:
<form class="form-watch-list-create">
<div ng-controller="MyCtrl" class="col-md-4">
<input
required="required"
placeholder="Movie title"
type="text"
class="form-control"
typeahead="item.Title for item in getLocation($viewValue)"
typeahead-loading="loadingLocations"
typeahead-min-length="2"
typeahead-wait-ms="1000"
typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html"
ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</form>
This puts the form around both columns.
Use the method attribute in form tag and end the form tag properly
<form role="form" method="get" class="form-watch-list-create">
<div ng-controller="MyCtrl" class="col-md-4">
<input
required="required"
placeholder="Movie title"
type="text"
class="form-control"
typeahead="item.Title for item in getLocation($viewValue)"
typeahead-loading="loadingLocations"
typeahead-min-length="2"
typeahead-wait-ms="1000"
typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html"
ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</form>
My modal for asking name
<?php session_start(); ?>
<div class="modal name fade modal-scrollable" id="name" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title myModalLabel">Your name!</h4>
</div>
<div class="modal-body">
<form id="nameForm" action="index.php" method="post">
<div class="form-group">
<label class="col-xs-3 control-label">Your name : </label>
<div class="col-xs-4">
<input type="text" class="form-control name" name="name" required/>
</div>
</div></div><br/><br/>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" id="ok">Ok</button>
</div>
</form>
</div>
</div>
</div>
Script code
<?php
if(!$_SESSION['user']['name']){
echo "<script>
$(window).load(function(){
$('#name').modal({
backdrop: 'static',
keyboard: true
})
$('#name').modal('show');
});
</script>";
}
?>
<script>
$('#ok').submit(function(){
<?php
$_SESSION['user']['name']=$_POST['name'];
print"location.reload();";
?>
});
</script>
This code actually works fine. The code is that on page load it will ask the name of user and set the session variable to set the entered name. It will open the modal only if session variable is not set. But in reality the user has to enter his name twice.I even tried reload() but it didnt work. Please help!
Your session is being set correctly. However, you are not initializing the session. The first line after your <?php tag should be:
session_start();
Please, also note that in this part of your code, you should print your JavaScript code:
<?php
$_SESSION['user']['name']=$_POST['name'];
print "location.reload();";
?>
I have some portlets here that has an ID for each of it. What I'm trying to do is to pass these IDs to a popup modal after pressing an Add button in the portlet.
Here is the portlet view:
#foreach($portlets as $portlet)
<div class="box span4">
<div class="box-header well" data-original-title>
<h2><i class="icon-th"></i> {{$portlet->portlet_title}} </h2>
</div>
<div class="box-content">
<div class="row-fluid">
// some contents over here
</div>
<div class="box-icon">
<i class="icon-plus-sign"></i>
</div>
</div>
</div>
#endforeach
Notice the Add New Link button at the bottom of the code, when I press that, a modal will appear. Here is the view of it:
<div class="modal hide fade" id="linkSettings">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Add New Link</h3>
</div>
<form method="post" action="{{ URL::to('addlink') }}" >
<div class="modal-body">
<div class="control-group">
<label class="control-label" for="focusedInput">Link Title:</label>
<div class="controls">
<input class="input-xlarge focused" id="link_title" name="link_title" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="focusedInput">Add A Link:</label>
<div class="controls">
<input class="input-xlarge focused" id="link_url" name="link_url" placeholder="eg. http://www.example.com/" type="text">
</div>
</div>
</div>
<div class="modal-footer">
Close
<input type="submit" id="submit" name="submit" value="Submit" class="btn btn-primary">
</div>
</form>
</div>
This modal has a form in it to be processed in the controller. But how do I send the portlet ID to this modal so that it can be stored in the database during processing in the controller? Because I want it to automatically know what portlet I want to add links in it. Any idea?
If anything, please let me know.
You could attach a jQuery event handler to clicking the "Add New Link" button, and use it to populate a hidden field in the modal view.
i.e. something like:
$("a.new_link").click(function(){
portlet_id = $(this).parent().parent().attr("id");
$("div.modal form input.my_hidden_field").val(portlet_id)
});
And somewhere in the <form> for the modal:
<input type="hidden" name="portlet_id" class="my_hidden_field"/>
Then, you just need to get the portlet_id POST parameter with Laravel/PHP to determine the portlet ID that was used.
Edit
Revised handler based on the code you gave:
$('.btn-settingLink').click(function(e){
e.preventDefault();
$('#linkSettings').modal('show');
// get the portlet_id and modify the midden field
portlet_id = $(this).parent().parent().attr("id");
$("div.modal form input.my_hidden_field").val(portlet_id)
});