I have received the following response from an API.
[{
"BookId":"32c03594-1ecb-4f97-8453-5b28a03d26d9",
"BookName":"Book1",
"Bookstatus":3,
"Country":"AU"
},
{
"BookId":"51d16696-b98a-4b3b-ac67-f36559cff70b",
"BookName":"Book2",
"Bookstatus":3,
"Country":"AU"
},
{
"BookId":"7b557a75-bf5e-4c29-9f31-43a7fee77520",
"BookName":"Book3",
"Bookstatus":3,
"Country":"AU"
},
{
"BookId":"c945ade5-d540-4378-9979-3842a1da396b",
"BookName":"Book4",
"Bookstatus":3,
"Country":"AU"
},
{
"BookId":"814869e2-e5af-48bc-a6da-28f272366955",
"BookName":"Book5",
"Bookstatus":3,
"Country":"AU"
}]
I have a webform with drop down box to choose the book you want.
What I want to happen, is when you click submit you are sent to another page, and on that page I want to be able to access the BookId, BookName and access token from the first page via $_POST. I have no trouble with the BookName but can't work out how to get the BookId and access token to go with it.
This is what I have so far:
Please note: the access token was obtained with $_GET in the head of the page and assigned to $access_token variable.
<body>
<?php
$array=json_decode($response, true);
$arr_len = count($array);//length of the array = 5
$for_len = $arr_len - 1;//length - 1
?>
<h1>CashBooks</h1>
<form action="getcoa.php" method="post">
<select name="books">
<?php
for($i=0; $i<=$for_len; $i++){
$bookname = $array[$i]["BookName"];
echo '<option value="' . $bookname . '" name="' . $bookname . '">' .
$bookname . '</option>';
};
?>
</select>
<input type="hidden" name="<?php $bookname; ?>">
<input type="hidden" name="<?php $access_token; ?>">
<input type="submit" value="Get Chart of Accounts">
</form>
</body>
With the BookId, I haven't figured out how to deal with that yet, so any suggestions would be great. And what I'm trying with the access token doesn't work. When I click submit with things how they are, I get this error on the "getcoa.php page":
"Notice: Undefined index: access_token in C:\wamp\www\getcoa.php on
line 6"
This is line 6:
$access_token = $_POST['access_token'];
FYI. The token part is solved.
But I dont think I have been clear on what I'm trying to accheive with BookId. On this page, the only thing the "USER" will see is a drop down menu with a list of BookNames, from which they select ONE, and click a submit button... On the next page, I will make a call to an API endpoint, but I need the BookId to call on that endpoint.
So the BookId for the selected BookName is needed to be somehow posted to the next page, so I can use $_POST on that page to get the BookId and use it to access the endpoint desired, without the end user knowing "BookId" exists in anyway.
Hopefully I have been more clear on what I'm trying to achieve. Sorry if I wasn't to start with.
If I have understood correctly then you are trying to send various parameters to a PHP script for processing depending upon which item from a dropdown menu is selected? Whilst there are a finite number of attributes an option tag can take you can of course use a dataset attribute - or, as in the code below, several.
So, with the help of some simple javascript, the hidden fields are populated when the select menu changes - the values stored in the hidden fields will be the ones sent to the PHP script when the form is submitted.
Previously the hidden fields had their names derived from the contents of the original source data ( the values ) - this would make processing at the server tricky as you would not necessarily know the names to use in the PHP logic tests etc - hence they are assigned names that correspond to the keys from the source data - that way you know to process fields called bookid,bookname & access-token
<?php
/* for test porpoises only */
$access_token=uniqid();
$response='[{
"BookId":"32c03594-1ecb-4f97-8453-5b28a03d26d9",
"BookName":"Book1",
"Bookstatus":3,
"Country":"AU"
},
{
"BookId":"51d16696-b98a-4b3b-ac67-f36559cff70b",
"BookName":"Book2",
"Bookstatus":3,
"Country":"AU"
},
{
"BookId":"7b557a75-bf5e-4c29-9f31-43a7fee77520",
"BookName":"Book3",
"Bookstatus":3,
"Country":"AU"
},
{
"BookId":"c945ade5-d540-4378-9979-3842a1da396b",
"BookName":"Book4",
"Bookstatus":3,
"Country":"AU"
},
{
"BookId":"814869e2-e5af-48bc-a6da-28f272366955",
"BookName":"Book5",
"Bookstatus":3,
"Country":"AU"
}]';
?>
<html>
<head>
<title>Sending values</title>
<script>
function getelement( name ){
return document.querySelectorAll('form input[name="'+name+'"]')[0];
}
function setvalues( e ){
var n=e.options[ e.options.selectedIndex ];
getelement('bookname').value=n.value;
getelement('access-token').value=n.dataset.token;
getelement('bookid').value=n.dataset.id;
}
</script>
</head>
<body>
<h1>CashBooks</h1>
<form action="getcoa.php" method="post">
<select name="books" onchange="setvalues(this)">
<optgroup label='Books'>
<option disabled='' hidden='hidden' selected=true>Please Select
<?php
$json=json_decode( $response );
foreach( $json as $obj ){
echo "<option value='{$obj->BookName}' data-id='{$obj->BookId}' data-token='{$access_token}' data-status='{$obj->Bookstatus}'>{$obj->BookName}";
}
?>
</optgroup>
</select>
<!-- empty, hidden input fields to be populated when dropdown changes -->
<input type="hidden" name="bookid">
<input type="hidden" name="bookname">
<input type="hidden" name="access-token">
<input type="submit" value="Get Chart of Accounts">
</form>
</body>
</html>
Multiple problems found in your script:
In the select box loop, see how to write a select box
https://www.tutorialspoint.com/html/html_select_tag.htm
Fault:
echo '<option value="' . $bookname . '" name="' . $bookname . '">' .
$bookname . '</option>';
It must be something like, and there is no name property required in the options
echo '<option value="' . $bookname . '">' . $bookname . '</option>';
:: Doubts you need to pass the bookid instead of bookname
In the input boxes, you haven't echo the variables,
Instead of
<input type="hidden" name="<?php $bookname; ?>">
<input type="hidden" name="<?php $access_token; ?>">
You need
<input type="hidden" name="bookname" value="<?php echo $bookname; ?>">
<input type="hidden" name="access_token" value="<?php echo $access_token; ?>">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
$response = '[{"BookId":"32c03594-1ecb-4f97-8453-5b28a03d26d9","BookName":"Book1","Bookstatus":3,"Country":"AU"},{"BookId":"51d16696-b98a-4b3b-ac67-f36559cff70b","BookName":"Book2","Bookstatus":3,"Country":"AU"},{"BookId":"7b557a75-bf5e-4c29-9f31-43a7fee77520","BookName":"Book3","Bookstatus":3,"Country":"AU"},{"BookId":"c945ade5-d540-4378-9979-3842a1da396b","BookName":"Book4","Bookstatus":3,"Country":"AU"},{"BookId":"814869e2-e5af-48bc-a6da-28f272366955","BookName":"Book5","Bookstatus":3,"Country":"AU"}]';
$array=json_decode($response, true);
$arr_len = count($array);//length of the array = 5
$for_len = $arr_len - 1;//length - 1
?>
<h1>CashBooks</h1>
<form action="getcoa.php" method="post">
<select name="books">
<?php foreach ($array as $key => $value) { ?>
<option value='{"<?php echo $value['BookName'] ?>":"<?php echo $value['BookId']; ?>"}'><?php echo $value['BookName'] ?></option>
<?php } ?>
</select>
<input type="hidden" name="<?php echo $bookname; ?>">
<input type="hidden" name="<?php echo $access_token; ?>">
<input type="submit" value="Get Chart of Accounts">
</form>
</body>
</html>
And on page getcoa.php can do this json_decode($_POST['books']);
I have a php file that has 3 forms inside and some insert queries and i separate them with if(isset()) based on what variable comes from $_POST. I want to make the forms appear for example on the center of the page and when the user presses submit the form fades out and the next one from the isset appears. How can i do that?
For example this is the first form
<?php if (!isset($_POST['date'])): ?>
<?php if (!isset($_GET['id'])):?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
Date :
<input type="text" name="date" id="date"> </br>
<input type="submit" value="Submit" />
</form>
<script>
$("#date").datepicker();
</script>
<?php endif;?>
<?php endif;?>
And then if the user presses submit there is an if(isset()) with an insert query.
After the insert query there is another form
<?php if (isset($_GET['id'])): ?>
<form action="<?php echo $_SERVER['PHP_SELF']?><?php echo " ?id=" . $_GET['id'] . " &date=" . $_GET['date'] . " &seats=" . $_GET['seats']; ?>" method="post">
Ώρα :
<input type="text" name="time">
<input type="submit" name="submit">
</form>
<?php endif;?>
So what i want is to display them on the center of my page but i dont have to open another page every time i press submit and they will fadeout (or something similar) and display the next form.
Sounds like you could do this with css. Not sure I understand your question though.
display all 3 forms on the page at once -> no isset stuff, I don't get what that is there for
hide the ones you don't want the user to see with a class ".hide"
submit the forms with jquery, as you don't want the page to reload. after submitting, hide the one you just sumbmitted by setting its class to hide and removing hide from the next form
form{
transition: opacity 2s;
}
form.hide{
opacity:0;
}
I have three pages. One of which there is a list of texts the user can select. Upon clicking on one of the texts they will be redirected to another page by using:
<a href='second.php?text=whatever>Whatever</a>
A page where they will input the username they wish to send those texts to - using forms. I wish to proceed to the third page with those two variable - texts and username. I only manage to proceed to third page with username only.
I am getting third.php?username=inputtedUsername.
I want to get third.php?username=inputtedUsername&&text=whatever.
I am aware that I can do by storing the text to a SESSION on page two and than transfer it over to third page.
I wish to know if there is another secure way to do this - maybe something needed to be changed in the form action=thirdpage.php? I dont know. Thank you. ö.ö.
Solved: After reading comments and answer, the thing I need was type=hidden. It is now working on my part. Thanks everyone for helping me. :).
'second.php?text=whatever'? You can't just put whatever to the text, you are doing it wrong. Try this.
firstpage.php
<?php
$whatever = 'Tom & Jerry, 1 + 2 = 3';
echo '' . $whatever . '';
?>
secondpage.php
<form action="thirdpage.php" method="post">
<input type="text" name="username" value="" />
<input type="hidden" name="text" value="<?php echo base64_decode($_GET['text']); ?>" />
<input type="submit" value="Submit" />
</form>
thirdpage.php
<?php
echo 'Username: ' . $_POST['username'];
echo '<br />';
echo 'Text: ' . $_POST['text'];
?>
I hope you can help me out, I am using a Wordpress Theme. It comes with a page template of a form. You can see the form functioning at my url:
http://getpaidtotryitfree.com/contactenos/
This is a URL for a different project. I took it down so I could put my client´s website up. I cannot figure out how to change the fields to show up in spanish. Here is the code:
http://getpaidtotryitfree.com/wp-content/themes/13Floor/contact.txt
<input type="text" name="et_contact_name" value="<?php if ( isset($_POST['et_contact_name']) ) echo esc_attr($_POST['et_contact_name']); else esc_attr_e('Name','13floor'); ?>" id="et_contact_name" class="input" />
So I went through all the "values" and chanded the part in parenthesis to ('Nombre','13floor') and so on for all other values.
When I did that the field wouldn´t dissapear on click, like it does right now, so I thought "thats alright" i´ll just highlight it and fill it in but once I did that it activated the error "Please Fill In All Fields" so it didnt accept that. Ok...So then I did:
Press Control + F, to find all the places with "et_contact_name" and changed it to "et_contact_nombre", I started with:
<input type="text" name="et_contact_**nombre**" value="<?php if ( isset($_POST['et_contact_**nombre**']) ) echo esc_attr($_POST['et_contact_**nombre**']); else esc_attr_e('Nombre','13floor'); ?>" id="et_contact_**nombre**" class="input" />
Then with the Control + F command I found all others like:
$et_site_name = is_multisite() ? $current_site->site_name : get_bloginfo('name');
wp_mail($et_email_to, sprintf( '[%s] ' . esc_html($_POST['et_contact_subject']), $et_site_name ), esc_html($_POST['et_contact_message']),'From: "'. esc_html($_POST['et_contact_**nombre**']) .'" <' . esc_html($_POST['et_contact_email']) . '>');
And did the same process with email,subject,message,etc. No luck though, the form didnt work then, it did change the fields, but it was not functional. (The highlighting deal I told you earlier)
I was able to change:
<input class="et_contact_submit" type="submit" value="<?php esc_attr_e('**Enviar**','13floor'); ?>" id="et_contact_submit" />
Which was "submit" this changed the text in the button, and left the form functional, I wish all other fields were as simple lol.
Ok and aside from this, there is no place in the .php where I can see how to change the error activations like:
Fill Name field
Fill Email Address field
Invalid email
Fill Subject field
Fill Captcha field
Fill Message field
I only see this piece of code:
$et_contact_error = true;
} else if ( empty($_POST['et_contact_name']) || empty($_POST['et_contact_email']) || empty($_POST['et_contact_subject']) || empty($_POST['et_contact_message']) ){
$et_error_message .= '<p>' . esc_html__('**Make sure you fill all fields**. ','13floor') . '</p>';
$et_contact_error = true;
But "Make sure you fill all fields" never even comes up when using the actual form, so as you can see I am completely lost I hope you can help me out, I would greatly appreciate it.
Thanks for contacting us
The default values for the fields are stored in
http://getpaidtotryitfree.com/wp-content/themes/13Floor/epanel/page_templates/js/et-ptemplates-frontend.js?ver=1.1
lines 67-70.
UPDATE
Yes, you should also translate the values in the form, you can find them at
... else esc_attr_e('XXXXX','13floor'); ...
and at
... esc_textarea('Mensaje','13floor'); ...
Example:
...
<input type="text" name="et_contact_name" value="<?php if ( isset($_POST['et_contact_name']) ) echo esc_attr($_POST['et_contact_name']); else esc_attr_e('Nombre','13floor'); ?>" id="et_contact_name" class="input" />
...
<input type="text" name="et_contact_email" value="<?php if ( isset($_POST['et_contact_email']) ) echo esc_attr($_POST['et_contact_email']); else esc_attr_e('Email','13floor'); ?>" id="et_contact_email" class="input" />
...
<input type="text" name="et_contact_subject" value="<?php if ( isset($_POST['et_contact_subject']) ) echo esc_attr($_POST['et_contact_subject']); else esc_attr_e('Sujetos','13floor'); ?>" id="et_contact_subject" class="input" />
...
<textarea class="input" id="et_contact_message" name="et_contact_message"><?php if ( isset($_POST['et_contact_message']) ) echo esc_textarea($_POST['et_contact_message']); else echo esc_textarea('Mensaje','13floor'); ?></textarea>
I've been having a rather irritating issue regarding capturing SQL information and then placing it into a PHP form (in theory, it should be kinda easy).
Here's the code for the SQL database information:
<?
$select = "SELECT * FROM beer WHERE country_id = 3";
$data = mysql_query($select) or die("Unable to connect to database.");
while($info = mysql_fetch_array($data)) {
echo '<center>';
echo '<h2>'.$info['name'].'</h2>';
echo '<table style="padding:0px;"><tr>';
echo '<tr><td><b>ABV%:</b></td><td width="570">'.$info['abv'].'</td></tr>';
echo '<tr><td><b>Bottle Size:</b></td><td width="570">'.$info['bottleSize'].'</td></tr>';
echo '<tr><td><b>Case Size:</b></td><td width="570">'.$info['caseSize'].'</td></tr>';
echo '<tr><td><b>Price:</b></td><td width="570">$'.$info['price'].'</td>';
echo '</tr></table>';
echo '</center>';
echo '<br/>';
echo '<img src="" border="0"><br><br>';
echo '<form name="cart" method="post" action="cart.php"> <table border="0"> <tr>';
echo '<td><input type="hidden" name="bname" value="'.$info['name'].'"><input type="hidden" name="price" value="'.$info['price'].'"></td>';
echo '<td><b>Quantity:</b></td>';
echo '<td><input type="text" name="qty" size="3"></td>';
echo '<td><input type="submit" value="Add to Cart" a href="cart.php?name=foo&price=bar" /a></td>';
echo '</tr></table></form>';
}
?>
I want when the submit value is pressed to somehow transmit the price, quantity and name to a basic HTML form (so that all the user has to do is add name, address, etcetc). I am completely stumped on how to do this.
If anyone could help, it would be much appreciated.
As you mentioned Amazon checkout, here is one thing you probably don't understand.
Amazoin doesn't use the form to move items data between server and browser to and fro.
It is stored in a session on a server time. All you need is some identifier put into hidden field.
To use a session in PHP you need only 2 things:
call session_start() function before any output to the browser on the each paghe where session needed.
Use `$_SESSION variable.
That's all.
Say, page1.php
<?
session_start();
$_SESSION['var'] = value;
and page2.php
<?
session_start();
echo $_SESSION['var'];
You wrote that code? because it's simply the same code as here.
You'll need to write an HTML form in your cart.php file
and use the $_POST variable to show the values of the price , quanitity and name.
For example:
<form method='post'>
<input type='text' name='price' value='<?=$_POST['price']?>'>
<input type='text' name='quanitity' value='<?=$_POST['qty']?>'>