How I can pass multiple parameters to URL in php - php

I have a problem in passing 2 parameters via URL
I don't know how, so I wrote this:
<form method="get" >
<b>Enter the Quantity you want:</b>
<input type="text" name="quantity">
</form>
<br><br>
<a href='./shopping-cart.php?part_id="<?php echo $_GET['part_id']; ?>"&quantity="<?php echo $_GET['quantity']; ?>"'>
<img src="add_to_shopping_cart.png">
</a>
$_GET['part_id'] this var from another URL and I want to pass it again and $_GET['quantity'] quantity from form.

You must use urlencode function for your parameters, and don't use the double quotes:
<a href='./shopping-cart.php?part_id=<?php echo urlencode($_GET['part_id']); ?>&quantity=<?php echo urlencode($_GET['quantity']); ?>'>
Even though, I suggest you to avoid long urls and use POST with hidden fields.

You get this notice Undefined index: quantity cause you are trying to access an undefined variable.
If your page url is something like page_name.php?quantity=5 then $_GET['quantity] is set, otherwise it isn't.
You should check if the $_GET variable exist.
If so, use #user4035 answer to print the url.
if(isset($_GET['quantity']))
{
//html code,the url
//echo $_GET['quantity']
}

There's a few errors that I can see.
You can't use $_GET['quantity'] if the user is entering it in without reloading the page (Remember, PHP is not client-side). Thus, I suggest using JavaScript for that.
Javascript:
function buildUrl(a) {
var qty = document.getElementById("qty").value;
a.href = "./shopping-cart.php?part_id="+<?php echo $_GET['part_id']; ?>
+"&quantity="+qty;
}
As you no longer need to get a PHP variable from the current page, the form is obsolete and a simple link will do.
HTML:
<b>Enter the Quantity you want:</b>
<input type="text" name="quantity">
<a href='#' onclick="buildUrl(this);"> //onclick attribute triggers the JavaScript
<img src="add_to_shopping_cart.png">
</a>

Related

Passing value of variable to next page using post method

In a code below I want to pass variable $user_id to next page using post method suppose my next page is followers.php . The whole code is link itself i want to put post method in it so that on clicking the link, variable is also passed to follower.php . Don't want to use sessions.
<a href='follower.php' style='text-decoration:none;'>
<h2>Followers</h2>
<?php
$checkfollowers = "SELECT * FROM follow_user WHERE user_id='$user_id'";
$resultfollowers = mysqli_query($con,$checkfollowers);
echo mysqli_num_rows($resultfollowers );
?></a>
I guess code for post method is correct:
<form method="get" action="follower.php">
<input type="hidden" name="varname" value="user_id">
<input type="submit">
</form>
So how to put this post method inside the other code so on clicking, follower.php is opened and variable is also passed to this page.
You are using GET method. Change it to
<form method="POST" action="follower.php">
<input type="hidden" name="user_id" value="user_id">
<input type="submit">
</form>
Also change the name to user_id of the hidden field. Now receive as
<?php
$user_id = $_POST['user_id'];
$checkfollowers = "SELECT * FROM follow_user WHERE user_id='$user_id'";
$resultfollowers = mysqli_query($con,$checkfollowers);
echo mysqli_num_rows($resultfollowers );
?>
Note: Above query is vulnerable to SQLI. Better to use Prepared statements.
You are talking about post method but in your code you are using the method GET. You might want to consider to change this into POST. This way the stuff filled out in the form will not enter the URL. (bit more secure). After this you could call the global variable $_POST['user_id']
A link cannot POST data, only GET.
use:
<a href='follower.php?user_id=1' style='text-decoration:none;'>
and in follower.php file:
$user_id = $_GET['user_id'];

Get slider values from Jquery to PHP in different pages (Bootstrap-slider-master)

I have a site with 3 images (arrows) that send you to another page, therefore:
index.html
-page1.php
-page2.php
-page3.php
For each there are a slider with bootstrap:
<input id="ex1" data-slider-id='exSlider' type="text" data-slider-min="0" data-slider-max="10" data-slider-step="1" data-slider-value="5" data-slider-handle="triangle" style="width: 100%;"/>
<input id="ex2" data-slider-id='exSlider' type="text" data-slider-min="0" data-slider-max="10" data-slider-step="1" data-slider-value="5" data-slider-handle="triangle" style="width: 100%;"/>
<input id="ex3" data-slider-id='exSlider' type="text" data-slider-min="0" data-slider-max="10" data-slider-step="1" data-slider-value="5" data-slider-handle="triangle" style="width: 100%;"/>
I want to send the value of the slider to the other PHP page, for example if I click in the first image and the slider in in 8, when I go to page1.php, I want to see the slider number.
From the index.html, I got the slider values as follows:
$(".slider").click(function(){
var sli1 = $("#ex1").data('slider').getValue();
});
I tough in use $_SESSION to store the values of each slider, but I don't know how to implement it, because the the way to get the slider value is on Jquery and the second page is on PHP.
So, how I store a value in Jquery site and then I access through PHP in different site?
Edit:
I don't get to retrieve the values from the second page. My folder looks like:
-index.html
<tr>
<td>
<div>
<p>ABC</p>
<input id="ex1" data-slider-id='Exsliser' type="text" data-slider-min="0" data-slider-max="10" data-slider-step="1" data-slider-value="5"/>
</div>
</td>
<a class="slider" href="slider/slider1.php"><img ></a>
In the same inside :
$(".slider").click(function(){
$sliderID = $(this).closest('div').find('input').attr('id');
$sliderValue = $(this).closest('div').find('input').data('slider').getValue();
alert($sliderID +' - '+$sliderValue);
//Send values to PHP script that updates them in $_SESSION
$.post("session_val.php",{sliderID:$sliderID, sliderValue:$sliderValue});
});
And in slider1.php:
<?php
session_start();
$val = $_SESSION['ex1'];
echo "<script type='text/javascript'>alert('$val');</script>";
?>
I have put session_start(); at the beginning of both files. The session_val.php file is in the same folder that index, but in other folder that slider1.php
For the arrow buttons that take you to another page, you could fire them with Javascript using "onclick". That would let you grab the value of the slider and pass it as a GET variable in the URL.
The below code assumes all the pages are in the same directory, so uses relative paths. You can easily adapt to your actual file structure -- or use absolute paths if necessary.
HTML:
<a onclick="get_slider('page2','ex2')">Page 2</a>
JS:
function get_slider(page,sliderID) {
//Get value of specified slider
$slideValue = $("#" + sliderID).data('slider').getValue();
//Build the url, with page name, then slider value passed as parameter
$url = page + ".php?slider=" + $slideValue;
//Send user to the proper page
window.location = $url;
}
PHP:
On the destination page, you can get the slider value like this:
<?php $sliderValue = $_GET['slider']; ?>
EDIT: Reflecting my comment below, forget the stuff above and use ajax instead:
slider-value.php
Simple script that takes two passed variables and updates the related $_SESSION variable.
<?php
/*** Your database connection string here ***/
//Get passed values
$sliderID = $_POST['sliderID'];
$sliderValue = $_POST['sliderValue'];
//Put them into $_SESSION variable
$_SESSION[$sliderID] = $sliderValue;
?>
Javascript:
$('[data-slider-id]').change(function() {
//Get slider's ID and value
$sliderID = $(this).attr('id');
$sliderValue = $(this).data('slider').getValue();
//Send values to PHP script that updates them in $_SESSION
$.post( "slider-value.php",{sliderID:$sliderID, sliderValue:$sliderValue});
});
I would suggest you wrap the input in a form.
<form action="page2.php" method="post">
<input type="range" name="input1">
<button type="submit">Next page</button>
</form>
This way when the user submits the form you will have access to the value in the input via $_POST['input1'].
You can take a similar approach for the rest of the pages. You are basically implementing a multistep form.

PHP variable inside a Javascript function outputs the variable and not the value

What I want to do is that only when I click on a div with id="$project['slug']", it will load the iframe inside that div.
So, i removed the src attribute from the iframe, and add it on onclick event.
I have this in my HTML/PHP:
<div class="box" id="<?=$project['slug']?>" onclick="load_frame();">
<iframe id="frame_<?=$project['slug']?>" frameborder="0"></iframe>
</div>
Js:
function load_frame() {
id = location.hash.replace('#', '');
var source = "<?php echo $project['video']; ?>";
$('#frame_'+id).attr("src", source);
}
Unfortunately, when I inspect the iframe it shows: src="<?=$project['video']?>" instead of the value that the variable holds.
Do you have any idea what i am doing wrong?
thank you!
jQuery is a client side language and have access only to the DOM elements once they have been rendered. So what you need to do is store $project['video'] variable in a hidden field and then using the id of that field get access to the rendered data.
Also, i noticed that you should use <?php instead of <?
You may try something like this.
<div class="box" id="<?php echo $project['slug']; ?>">
<iframe id="frame_<?php echo $project['slug']; ?>" frameborder="0"></iframe>
<input type="hidden" id="<?php echo $project['slug']; ?>" value="<?php echo $project['video']" />
</div>
Then in jQuery do:
$(document).ready(function(){
$('.box').click(function(){
var slug = $(this).attr('id');
var source = $('input#' + slug).val();
$('iframe#' + slug).attr("src", source);
});
});
add hidden input on html page
<input type="hidden" id="hidsource" value="<?php echo $project['video']" />
edit your function in js like this
function load_frame() {
id = location.hash.replace('#', '');
$('#frame_'+id).attr("src", $('input#hidsource').val());
}
hope this will work
You might not have the shorthand syntax enabled on the server. Try standard PHP instead:
<?php echo $project['slug']; ?>

isset for link - PHP

I'm attempting to create a link for users to click that will remove them from a list. I'm trying to figure out how to do this without using a submit button and without using $_GET(if possible).
Anyway, I'm afraid to do it with $_GET (the way I have it now), because the user can type this in the URL (even though 99% wouldn't know how or think to do this) and they would be removed from the list.
How can I name the link so I can use $_POST?
$attendingUsers = mysql_query("Select acceptedInvites from events where eventID = ".mysql_real_escape_string($_GET['eventID'])." ");
$users= mysql_fetch_array($attendingUsers);
$user = $users['acceptedInvites'];
if(preg_match("/$userid/", $user)){
echo "You are attending this event</br>";
echo 'Click here to remove yourself from the list';
if($_GET['delete']=1){
$sql=...
}
}
Is it possible to do this without using $_GET? Thanks!
Never delete via a link. Read The Spider of Doom
Best way is to link to a "delete" page with an "are you sure" form. Submitting the form (via POST) performs the delete and redirects back to a suitable results page.
For example
Click here
to remove yourself from the list
Then, in remove.php
<?php
// get Event details via $_GET['eventID']
if (isset($_POST['confirm'])) {
// delete via SQL
// redirect
header('Location: http://example.com/events.php');
exit;
}
// display event details
?>
<form method="post" action="remove.php?eventID=<?php echo $eventId ?>">
<p>Are you sure?</p>
<input type="submit" name="confirm" value="Remove me from this event">
</form>
You should probably also look into CSRF protection but that's really outside the scope of this question.
Your are required to use either $_GET or $_POST
<form action="delete.php" method="post">
<input type="hidden" name="eventId" value="yourEventId" />
<a href="#" onclick="this.form.submit();" > Delete</a>
</form>
If I have my JavaScript right, this should do the trick:
Delete
<form id="delete" action="delete.php" method="post">
...
</form>
The link will then submit the form.
You could use some kind of encoding to make the get var unreadable, like an md5 or even an encrypted string.

Basic problem of getting values of my textbox

I am just trying to learn PHP and want to get the value of the textbox using $_post function, but its not working. I am using wamp 2.1 and the code is simple as below
<form method="POST" action="c:/wamp/www/test/try.php">
<input type="text" name="nco" size="1" maxlength="1" tabindex="1" value="2">
<input
tabindex="2" name="submitnoofcompanies" value="GO"
type="submit">
</form>
<?php
if (!isset($_POST['nco']))
{
$_POST['nco'] = "undefine";
}
$no=$_POST['nco'];
print($no);
However in no way I get the value of the textbox printed, it just prints undefined, please help me out.
You first assigned the word "undefine" to the variable $_POST['nco'].
You then assigned the value of the variable $_POST['nco'] (still "undefine" as you stored there) to the variable $no.
You then printed the value stored in the variable $no.
It should be clear that this will always print the word "undefine".
If you want to print the value of the textbox with the name nco, fill out the form with that textbox, and in the page that process the form,
echo $_POST['nco'];
...is all you do.
You need to setup a form or something similar in order to set the $_POST variables. See this short tutorial to see how it works. If you click the submit button, your $_POST variables will be set.
what for you are using this line $_POST['nco'] = "undefine"; } ..?
and please cross check whether you are using form method as post and make sure that your text name is nco ... or else use the below code it will work.
<?php
$no = $_POST['nco'];
echo $no;
?>
<form name='na' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type='text' name='nco'>
</form>
thanks
Your action is wrong.
Change it to
action="try.php"

Categories