passing form input name through hyperlink - php

I am trying to pass input tag value to the another page to simply multiply the value of qty with price and return the updated price.The problem is when i pass the value of qty with get method it passes the value of qty only but does not pass another values.
cart.php
echo" <form method='get' name='form1' action='update_cart.php?id={$id}&name={$name}&price={$price}&qty=$_GET['qty']'>";//the problem comes here.
echo"<input type='number' name='qty' max='10'>
<input type='submit' value='update'></form>";
update_cart.php
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$qty=isset($_GET['qty'])? $_GET['qty']: "";
$price=isset($_GET['price'])? $_GET['price']: "";
$price=$price*$qty;
header('Location: cart.php?action=quantity_updated&id=' . $id . '&name=' . $name . '&price='.$price . '&qty='.$qty);
when i click on update button after giving qty a value it shows something like this.
http://localhost/abc/cart.php?action=quantity_updated&id=&name=&price=0&qty=2

form method="get" passes variables automatically.
You do (and can) not need to append query string to the action attribute.
You do however need to represent those data in your form, if with <input type="hidden" name="qty" value="<?=$qty?>" /> style.

if you want pass some values , you can choose 2 way for it :
you can use the input with hidden type in you form.
if you use hidden type input you can use data just on one page.
<form method='get' action='?????'>
<input type='hidden' name='?????' value='?????'>
<input type='hidden' name='?????' value='?????'>
....
...
..
<input type='number' name='qty' max='10'/>
<input type='submit' value='submit'/>
</form>
you can use session for save data and use it in another page.
if you use session, you can use data in all php pages.
on page 1:
<?php
session_start();
$_SESSION['name']='value';
?>
on page 2:
<?php
session_start();
echo $_SESSION['name']; // value
?>

Your syntax for this line contains errors.
echo" <form method='get' name='form1' action='update_cart.php?id={$id}&name={$name}&price={$price}&qty=$_GET['qty']'>";
Change it to...
echo" <form method='get' name='form1' action='update_cart.php?id=".$id."&name=".$name."&price=".$price."&qty=".$_GET['qty']."'>";

Related

Update DB after form submit

I am trying to carry a variable over to disable.php which will then update a row in the database, all of this is within a wordpress plugin I am building. I cannot see why this won't work.
Heres my form
<form method='post' action='".plugins_url()."/myremovalsquote/inc/disable.php''>
<input type='submit' name='".$_SESSION['id'] = $active_partner->partner_id."' class='button-primary' id='disable' value='Disable'/>
</form>
Heres my /disable.php
global $wpdb;
$id = $_SESSION["id"];
$wpdb->query("UPDATE partners SET active='no' WHERE partner_id='".$id."'");
header("Location: http://www.website.com/wp-admin/admin.php?page=my-plugin-settings");
This is the error I am getting, it seems that the variable from the session isn't being carried over to disable.php.
In addition to my comment, you could either use a hidden input field or store the values to be read and sent in the session alltogether. For the first solution:
<form method='post' action='/myremovalsquote/inc/disable.php'>
<input type="hidden" name="id_to_be_disabled" value="<?= $active_partner->partner_id; ?>">
<input type='submit' class='button-primary' id='disable' value='Disable'/>
</form>
For the second solution just invoke the session with session_start(); and store the value in it. No need to fiddle around with the submit button name.
You have some errors in your form with name and value. Try this:
<form method='post' action='".plugins_url()."/myremovalsquote/inc/disable.php"'>
<input type='submit' name='id' value='" . $active_partner->partner_id."' class='button-primary' id='disable' value='Disable'/>
</form>
And I also would prefer a hidden field here.
put session_start() on your /disable.php

I'm trying to use a textbox value as a session variable

I'm trying to use a textbox value as a session variable when the button is clicked, so far I have it working but it's hardcoded in, how do I get it to use the textbox value instead?
Here's the session code:
<?php
session_start();
$_SESSION['url'] = "url";
?>
Here's the textbox:
echo "<input type='text' id='starurl' value=''/>";
echo "<br><button onclick='save_a9({$row99['starID']})'>Approve</button><button onclick='save_d9({$row99['starID']})'>Disapprove</button><br>";
Here's the save_a9:
function save_a9(id) {
$.post('response6.php', {starID:id},
function(result) {
alert(result);
window.location.reload();
});
}
Is this what you mean? the value doesn't need a form, it just needs to go to another page to be used there
<?php
session_start();
if (isset($_POST['url']) {
$_SESSION['url'] = $_GET['url'];
}
?>
echo "<input type='text' id='starurl' value='" . htmlspecialchars($_SESSION['url'])"
When you are assigning the _SESSION url, you will need to use the posted variable to assign the string.
$_SESSION['url'] = $_GET['url'];
To do the opposite, and have the textbox show the value of the session, you would:
echo "<input type='text' id='starurl' value='" . htmlspecialchars($_SESSION['url']) . "'/>";
It is important to note that if you want the text box to actually do something, you will need to have the input box wrapped around a form tag.
The <form> tag tells the browser where the form starts and ends. You can add all kinds of HTML tags between the <form> and </form> tags. (thanks echoecho!)
If you are working RESTfully, GET should be used for requests where you are only getting data, and POST should be used for requests where you are making something happen.
Some examples:
GET the page showing a particular SO question
POST a comment
Click the "Add to cart" button and send a POST request.
(Thanks Skilldrick!)
The form & PHP file would look like this:
<?php
session_start();
if (isset($_POST['url']) {
$_SESSION['url'] = $_POST['url'];
}
echo '<form action="POST" method="?">';
echo "<input type='text' id='starurl' value='" . htmlspecialchars($_SESSION['url']) . "'/>";
echo '</form>';
You will notice when the form is updated, the session updates too. When you close your browser and open it again, you will see you old contents on the input box. (Press "enter" to save the input box without a submit button).
Simply use this one
//page1.php
<form action="page2.php" method="post">
<input type="text" name="session_value">
<input type="submit" name="set_session" value="Set Session">
</from>
//page2.php
<?php
if(isset($_POST['set_session'])){
session_start();
$_SESSION['url'] = $_POST['session_value'];
}
?>
For getting the value from your textbox, first modify your HTML as :
<input type='text' id='starurl' name='url' value=''/>
The textbox needs a name attribute which is used by the $_POST, $_GET or $_REQUEST superglobals.
Then depending upon your form submission method (GET or POST) :
$_SESSION['url'] = $_GET['url'] // Default method for form submission
$_SESSION['url'] = $_POST['url'] // For <form method="post">
$_SESSION['url'] = $_REQUEST['url'] // Works for both
EDIT :
Since you are using a button onclick event to set the session variable, you can use this (assuming you are using jQuery) :
Javascript:
$('button').click(function(){
var url = $('#starurl').val();
$('#sessionURLDiv').load('save_session_url.php?url='+url);
});
PHP: save_session_url.php :
<?php
session_start();
if ( isset ( $_GET['url'] ) ) {
$_SESSION['url'] = $_GET['url'];
}
?>
HTML :
Just add this div anywhere in the page.
<div id = "sessionURLDiv"></div>
I hope this helps.

request in while loop

I work with this code:
<form method="get" name="MobileDetails">
<input name="brand" id="brand" value="<?php echo $brand;?>" type="hidden">
<input name="brid" id="brid" value="<?php echo $brandid;?>" type="hidden">
<button type="button" name="submitButton" value="get Details" onclick="getDetails()">
</form>
java script
<script type="text/javascript">
function getDetails(){
var brand = document.getElementById('brand').value;
var brandid = document.getElementById('brid').value;
document.MobileDetails.action = 'details.php?brand='+brand+'&id='+brandid;
document.MobileDetails.submit();
}
</script>
But it does not work in while loop. Whats the problem? My code is given below.
When i click on the button it do not do anything. But the code work great with out while loop given on the top.
<?php
require_once('connection.php');
$SQL= "SELECT*FROM mobile ORDER BY price ASC LIMIT 10";
$result= mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)){
$brand=$db_field['brand'];
$id=$db_field['id'];
$model=$db_field['model'];
echo "<form method='get' name='MobileDetails'>";
echo " <input name='brand' id='brand' value='". $brand ."' type='hidden'>";
echo" <input name='brid' id='brid' value='". $id ."' type='hidden'>";
echo" <input name='mod' id='mod' value='". $model ."' type='hidden'>";
echo" <button type='button' name='submitButton' value='get Details' onclick='getDetails()'/>
</form> ";
echo "CLICK HERE";
}
?>
You're using several times the same id. Ids have to be unique.
Uou are dealing with multiple id's. The job of an ID is to be unique identifier for the element. I suggest just using
<form action="details.php" type="get">
this will do exactly what you are trying to achieve without using the function.
The thing with element ID's is that they need to be unique for the page; however, as you may see, not required for HTML to be displayed. When calling your JS function getDetails(), it grabs the element by ID but when you have multiple ID's in the page, this will fail.
So what can you do? Well, in your loop, you create a new form for each 'brand'. You can pass a reference of the form to the grabdetails and then, by NAME, grab the values from that form.
Rather than using Javascript to generate a link based on given details put in a hidden field, you should just generate the action at the PHP level.
echo "<form method='get' name='MobileDetails' action='details.php?brand=$brand&id=$brandid'>";
But since you do have hidden fields, using just action='details.php' the form will take the user to
details.php?brand={brand}&brid={id}&mod={model}
You should look into POST or making your button into a plain link rather than having a form.

pass php variables from one form to be submitted to database in another

I have a pop up box which checks if the user is signed in or not. If he is, I'm echoing out a small form which the user will press a button and it will submit to the DB. The variables are displayed on the popup but when pressed submit, they do not pass to the submit php file.
$add_wish = "<form action='memWishList.php' method='post' id='memWishList'>
<h3>Add this item to your Wish List?</h3><br>
<input type='hidden' name='title' value='".$title."'>".$title."</input><br>
<input type='hidden' name='link' value='".$link."'></input><br>
<input type='submit' name='submit' value='Add'/><button id='cancel'>
Cancel</button>
</form>";
echo $add_wish;
I want to pass the values title and link to be submitted to the DB. Here's my memWishList.php file:
if (isset($_POST['submit'])){
//get member id
$title = mysqli_real_escape_string($_POST['title']);
$link = mysqli_real_escape_string($_POST['link']);
$mysql = "INSERT INTO wish_list (memNum, title, link, date) VALUES ('$memnum', \
'$title', '$link', now())";
$myquery = mysqli_query($mysqli_connect, $mysql);}
Doing it this way, I only get the member id and the date inserted, not the title and the link. What's the problem? The reason why I'm echoing out this form is there's an if/else statement for logged in users and non logged in. Would be much easier to do it in html but can't...
DB: memnum(varchar), title(longtext), link(longtext), date(date). I have other tables where long links and titles are inserted just fine as longtext. They're coming from rss feeds.
please check documentation: mysqli_real_escape_string function expect the string as 2nd parameter if you use a procedural approach. It could be i.e.:
$link = mysqli_real_escape_string($mysqli_connect, $_POST['link']);
You have some markup errors. Your hidden input tags should look like:
<input type='hidden' name='link' value="<?php echo $link ?>">
Update your HTML file to look like this and all of the values will be sent to the $_POST variable:
<form action='memWishList.php' method='post' id='memWishList'>
<h3>Add this item to your Wish List?</h3><br>
<input type='hidden' name='title' value="<?php echo $title ?>"><?php echo $title ?><br>
<input type='hidden' name='link' value="<?php echo $link ?>"><br>
<input type='submit' name='submit' value='Add'/><button id='cancel'>Cancel</button>
</form>

How can I pass the value of a hidden text field to another php page?

I have a php page with the following:
echo "<form method='post'><input type='hidden' id='memberIdd' name='memberIdd' value=" . $memberId . "></form>";
Now I created another php page and want to grab the value of this hidden field and place it into another variable. How can I do this? I tried using this for my second php page:
$member = $_POST['memberIdd'];
But I just keep getting "undefined" for $member.
Thanks
I think your problem is that you did not set the right action in the form and you also should have a submit button. Thus, your form should look like:
echo '<form action="url-to-the-second-page.php" method="post">';
echo '<input type="hidden" id="memberIdd" name="memberIdd" value="' . $memberId . '">';
echo '<input type="submit" name="submit" value="submit" />';
echo '</form>';
<form method='post' action='secondpage.php'>
http://www.w3.org/TR/html4/interact/forms.html#h-17.1
If you don't write the action then the posted data goes to the same page where you were.
And by the way, the variable $memberId (the one in your question) at the firstpage.php should be defined. In other case it will prompt error message.
Edit: It will better for you to use HTML codes out of PHP.
That's a hidden form field, but you've no submit button. The value won't appear in the second page unless you specify it as the form's action (i.e. where the form should be submitted to), and also submit the form. e.g.
echo "<form action='page2.php' method='post'>
<input type='hidden' id='memberIdd' name='memberIdd' value=" . $memberId . ">
<input type="submit" value="Go to page 2">
</form>";
Alternatively, you could store the value as a session variable.

Categories