I am creating a page where users can submit metadata about content to a database by submitting the Digital Object Identifier (DOI) of the content. The site will then go and look up the metadata of content on www.crossref.org and present a summary of the data before adding it to the database
I have created a form for users to enter the DOI
<FORM ACTION="newref-getxml.php" METHOD=POST>
<P><strong>New Reference Form</strong><BR>
DOI: <INPUT NAME="send_doi"><BR>
<BR>
<INPUT TYPE=SUBMIT NAME="submitref" VALUE="SUBMIT">
</FORM>
And a file to fetch and read the XML (I have removed my API KEY from the URL for obvious reasons)
<?php
echo $_POST[send_doi]; // check post data is coming though
$xml = simplexml_load_file("http://www.crossref.org/openurl/id=doi:'$_POST[send_doi]'&noredirect=true&pid=APIKEY&format=unixref");
?>
<p>
Title: <?php echo $xml->doi_record->crossref->journal->journal_article->titles->title;?><br />
Year: <?php echo $xml->doi_record->crossref->journal->journal_article->publication_date->year;?><br />
Journal: <?php echo $xml->doi_record->crossref->journal->journal_metadata->full_title;?><br />
DOI: <?php echo $xml->doi_record->crossref->journal->journal_article->doi_data->doi;?>
</p>
The problem is with inserting the user submitted DOI into the URL, I thought I could just paste '$_POST[send_doi]' into the URL where the DOI should go, but that is not working.
All I get is
10.3998/3336451.0009.101
Title:
Year:
Journal:
DOI:
When submitting a DOI
How you write the URL to include the '$_POST[send_doi]' value?
simplexml_load_file("http://www.crossref.org/openurl/id".
"?doi=".urlencode($_POST[send_doi]).
"&noredirect=true&pid=APIKEY&format=unixref");
Added a question mark as I don't see it in your URL. A better alternative is http_build_query(). Check it out!
simplexml_load_file('http://www.crossref.org/openurl/id?'. // <- Question Mark here
http_build_query(array(
'doi' => $_POST[send_doi],
'noredirect' => 'true',
'pid' => 'APIKEY',
'format' => 'unixref',
))
);
Try this:
$xml = simplexml_load_file("http://www.crossref.org/openurl/id=doi:'{$_POST['send_doi']}'&noredirect=true&pid=APIKEY&format=unixref");
Array keys of type string need to be encapsulated in single quotes. When including a variable into a string, use {} to enclose the variable itself.
Also, make sure you validate that input so you dont have erraneous calls going to that API. Regex works well. See: preg_match().
Related
I need to show json data from dynamic url in my website. When visitor make input which is his loyalty card number that number is added to url as sufix. That url is url that contains his detals related to loyalty programme (points he earned so far etc.).
My code:
<form action="" method="get">
<input type="text" id="unos" name="card" value = ""><br><br>
<input type="submit" name="submit" value="Provjeri bodove">
</form>
<?php
$card = $_GET["card"];
$url='SERVER_URL_HERE/cardcheck/testAlsLoyalty.php?card= <?php echo $card; ?> ';
$json = file_get_contents($url);
$jo = json_decode($json);
echo $jo->user;
echo $jo->card;
echo $jo->rezultat;
echo $jo->bodova;
echo $jo->raspolozivo;
?>
What I got so far is that this code add input as suffix to url of my website, and if I put url of json website as form action value visitor is taken to json website where he can see data. But I want to load data from that url at my website (html or php) after he type card number.
Thank you in advance
You should check that the variable you're trying to use later on is actually set and also before you append the card number to the url, use rawurlencode() before submitting, otherwise that could break the url (when someone uses unallowed URL characters - i.e. spaces).
<form action="" method="get">
<input type="text" id="unos" name="card" value = ""><br><br>
<input type="submit" name="submit" value="Provjeri bodove">
</form>
<?php
if(isset($_GET["card"])){
$card = $_GET["card"];
$url='http://SERVER_URL_HERE/cardcheck/testAlsLoyalty.php?card=' . rawurlencode($card);
$json = file_get_contents($url);
$jo = json_decode($json);
echo $jo->user;
echo $jo->card;
echo $jo->rezultat;
echo $jo->bodova;
echo $jo->raspolozivo;
}else{
//Notify user here that no card number has been submitted
}
?>
Note that I removed the IP address and port from the URL and replaced with SERVER_URL_HERE. You should never ever publish your public IP address (or domain) as it just opens doors for attackers, especially when you directly give the endpoint...
You don't use <?php echo in strings. Use string concatatenation.
$url='http://81.93.93.78:8085/cardcheck/testAlsLoyalty.php?card=' . $card;
<?php echo ... ?> is used when you've exited PHP code with ?>, and want to insert a PHP expression in the output.
So this is my code for the first_name. I have no clue why it does not work whenever I add an entry.
First Name: <input type="text" name="first_name" value="<?php function convertString($first_name){
$first_name=htmlentities(strip_tags($first_name));
$lowercaseFName = strtolower($first_name);
$ucFName = ucwords($lowercaseFName);
return $ucFName;} ?>"/> // I changed it with echo but nothing changed.
My code doesn't change the format of the text.I tried inputting in all caps and it will show as is. Am I missing something or doing it wrong?
The inputted text if all in capital must still be shown as Camel Case format in the table, like this:
First Name: MARIA YLONA (in the form)
First Name: Maria Ylona (in the table) - this is another .php file for viewing of the data entries
Seeing you didn't post your html form or how it's used, am submitting the following as a successful piece of code and using !empty() against a POST array with form tags and an isset() for the submit input.
Btw, functions and variables assignments should be used seperately than in inputs/form elements.
Create a function, then pass it inside the input with the parameter.
<?php
function convertString($first_name){
$first_name=htmlentities(strip_tags($first_name));
$lowercaseFName = strtolower($first_name);
$ucFName = ucwords($lowercaseFName);
return $ucFName;
}
if(isset($_POST['submit'])){
if(!empty($_POST['first_name'])){
$first_name = $_POST['first_name'];
}
}
// here initialize $first_name to something meaningful
?>
<form method="post">
First Name: <input type="text" name="first_name" value="<?php echo(convertString($first_name)) ?>"/>
<input type="submit" name="submit" value="Submit">
</form>
Footnotes:
In regards to what Marc mentioned about McDonald's -> mcdonald's -> Mcdonald's and my MacDonald in comments...
Consult the following here on Stack which may prove to be useful:
Given upper case names transform to Proper Case, handling "O'Hara", "McDonald" "van der Sloot" etc
Data Cleanup, post conversion from ALLCAPS to Title Case
You mention the use of a database, but haven't posted relative code and would be beyond the scope of the question.
To clarify what aynber meant in the comments:
<?php
function convertString($first_name){
$first_name=htmlentities(strip_tags($first_name));
$lowercaseFName = strtolower($first_name);
$ucFName = ucwords($lowercaseFName);
return $ucFName;
}
// here initialize $first_name to something meaningful
?>
First Name: <input type="text" name="first_name" value="<?php echo(convertString($first_name)) ?>"/>
I'm having this nasty problem that I can't find an answer to. Basically I have multiple forms on my php page. I need to pass $_SESSION value based on which form visitor uses.
For example, if he uses one form, I need these values
session_start();
$_SESSION['socnetwork'] = '1';
$_SESSION['cost'] = '15';
$_SESSION['soccount'] = '100';
If he uses another form on that page, I need different values to be passed on submit="somepage.php", for example
session_start();
$_SESSION['socnetwork'] = '2';
$_SESSION['cost'] = '55';
$_SESSION['soccount'] = '700';
How would you recommend me to approach this? Variables need to be secure! I cannot pass them in URL, although this would be so convinient.
On the client-side, you may use an if statement (you would need JavaScript in this case) and send the required information to PHP server-side using a POST request.
On the server-side, you may create a handling code, which will set the required values to the $_SESSION variable.
Alternatively, if the values are fixed, you could send not the values but rather some integer or string code from client side to the server side (it could be done with plain HTML), and then use an if statement on the server-side (PHP in your case). In this case, that is also possible to insert the numbers to the HTML form with PHP like that:
<input type='hidden' name="category" value=" <?php echo $some_code; ?> " />
You may store arrays in sessions so make somethings like $_SESSION['custom_pages'] where each key is the name of page (or id) and values - are arrays of 'socnetwork', 'cost',..
session_start();
$_SESSION['form1'] = ['socnetwork'> = '1',
'cost' => '15',
'soccount' => '100];
....
....
$_SESSION['form2'] = ['socnetwork'> = '2',
'cost' => '55',
'soccount' => '700];
A submit button adds its name to the $_POST button, so if you name your buttons you know which button the visitor used.
E.g
<form action="somepage.php">
<input type="submit" name="form1" value="Submit!">
</form>
<form action="somepage.php">
<input type="submit" name="form2" value="Submit other form!">
</form>
<?php var_dump($_POST); ?>
would give
array(1) { ["form1"]=> string(7) "Submit!" }
array(1) { ["form2"]=> string(18) "Submit other form!" }
And from that point it is a matter of checking whether a certain key exists.
Users of my website can generate a custom form. All the fields are saved in a database with a unique ID. When someone visits the form, the fields 'name' attribute is field*ID*, for example
<p>Your favorite band? <input type="text" name="field28"></p>
<p>Your Favorite color? <input type="text" name="field30"></p>
After submitting the form, I use php to validate the form, but I don't know retrieve the value of $_POST[field28] (or whatever number the field has).
<?
while($field = $query_formfields->fetch(PDO::FETCH_ASSOC))
{
$id = $field[id];
//this doesn't work!!
$user_input = $_POST[field$id];
//validation comes here
}
?>
If anybody can help me out, it's really appreciated!
Add some quotes:
$user_input = $_POST["field$id"];
I'd suggest taking advantage of PHP's array syntax for forms:
<input type="text' name="field[28]" />
You can access this in php with $_GET['field'][28]
$user_input = $_POST['field'.$id];
Remember that you are using a string for the first part of the input name, so try something like: $user_input=$_POST['field'.$id];.
Also, I would suggest calling them into an array to retrieve all data:
<?php
$user_inputs=array();
while($field=$query_formfields->fetch(PDO::FETCH_ASSOC)) {
$id=$field['id'];
$user_inputs[]=$_POST['field'.$id];
}
?>
dear all,
I need to send parameters to a URL without using form in php and get value from that page.We can easily send parameters using form like this:
<html>
<form action="http://..../abc.php" method="get">
<input name="id" type="text" />
<input name="name" type="text"/>
<input type="submit" value="press" />
</form>
</html>
But i already have value like this
<?php
$id="123";
$name="blahblah";
?>
Now i need to send values to http://..../abc.php without using form.when the 2 value send to abc.php link then it's show a value OK.Now i have to collect the "OK" msg from abc.php and print on my current page.
i need to auto execute the code.when user enter into the page those value automatically send to a the url.So i can't use form or href. because form and href need extra one click.
Is their any kind heart who can help me to solve this issue?
You can pass values via GET using a hyperlink:
<a href='abc.php?id=123&name=blahblah' />
print_r($_GET) would then give you the values, or you can use $_GET['id'] etc in abc.php
Other approaches, depending on your needs, include using AJAX to POST/GET the request asynchronously, or using include/require to pull in abc.php if it only includes specific functioanlity.eg:
$id="123";
$name="blahblah";
require('abc.php');
You can do:
$id="123";
$name="blahblah";
echo "<a href = 'http://foo.com/abc.php?id=$id&name=$name'> link </a>";
<?php
$base = 'http://example.com/abc.php';
$id="123";
$name="blahblah";
$data = array(
'id' => $id,
'name' => $name,
);
$url = $base . '?' . http_build_query($data);
header("Location: $url");
exit;